C++ try catch 的用法

2025-05-06 09:46:51
推荐回答(1个)
回答1:

catch(object^)表示捕获一个object类型的异常类。而这个类必须是在try语句块中被抛出的。例如:
#include
#include
using namespace std;

int main()
{
string ex = "this is a exception";
try
{
cout<<"before throw"< throw ex;
cout<<"after throw"< }
catch(string &e)
{
cout< }

cout<<"end"<}

以上这个例子也说明了,你这 个问题用try,catch是不能解决的。捕捉到异常后,是不再执行下一条语句,当然如果你放在catch语句块之后,是可以执行的。

将抛出异常的语句放在try中,在catch中处理该异常,在catch之后继续执行就可以了。
希望可以帮助到你