![]() |
#2
LiGoudan2018-03-06 21:55
|
跟着书上的例子,看的也不是很明白,自己写了一个异常处理,但是抛出异常之后程序直接意外结束

#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
runtime_error err_1("1");
runtime_error err_2("2");
int choice;
cin >> choice;
switch(choice) {
case 1:
throw err_1;
case 2:
throw err_2;
default:
break;
}
try {
cout << choice << endl;
}catch(runtime_error err_1) {
cerr << err_1.what() << endl;
}catch(runtime_error err_2) {
cerr << err_2.what() << endl;
}
}
using namespace std;
int main(int argc, char *argv[]) {
runtime_error err_1("1");
runtime_error err_2("2");
int choice;
cin >> choice;
switch(choice) {
case 1:
throw err_1;
case 2:
throw err_2;
default:
break;
}
try {
cout << choice << endl;
}catch(runtime_error err_1) {
cerr << err_1.what() << endl;
}catch(runtime_error err_2) {
cerr << err_2.what() << endl;
}
}
刚开始写了一个程序,不会异常退出,但是后来给关了,就找不到备份了
想问一下这段代码问题出在哪里,输入 1 或者 2 之后抛出异常直接退出(什么原因导致捕捉不到错误)
输入:2
Terminated due to signal: ABORT TRAP (6)
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: 2