try和catch的用法_try catch-CSDN博客
try和catch是用于处理异常的语句,它们构成了一种异常处理机制。
在一个try语句中,程序执行一段代码,如果发生了异常,则会被捕获,并转到相应的catch语句中进行处理。
------
什么时候会出现异常?
C++异常详细介绍-CSDN博客
除0操作
发生越界
内存错误
--------------
异常不会终止程序,并且会将错误信息详细介绍。
error: no matching function for call to 'std::exception:exception(const char[16])' - zkfopen - 博客园 (cnblogs.com)
#include <QDebug>
#include <iostream>
#include <exception>
using namespace std;int test(){int a = 1;int b = 0;if (b == 0){throw bad_exception();//抛出异常}return a / b;
}int main()
{try{qDebug() << test();}catch (const exception& e){qDebug() << "异常";qDebug() << e.what();}system("pause");return 0;
}
异常
std::bad_exception
请按任意键继续. . .