- 標準例外類別
一切衍生自exception所以要catch 以下exception
exception
bad_alloc
bad_exception
ios:base:failure
bad_cast
bad_typedid
logic_error
lehgth_error
domain_error
invalid_argument
out_of_range
runtime_error
overflow_error
range_error
underflow_error
先#include
using namespace std;
int main() {
try {
string str("caterpillar");
cout << "str.substr(20, 2) = "
<< str.substr(20, 2)
<< endl;
}
catch(out_of_range e) {
cout << "錯誤: "
<< e.what() //what方法取得錯誤訊息
<< endl;
}
return 0;
}
#include
#include
using namespace std;
int main() {
double *ptr;
do {
try {
ptr = new double[1000000];
}
catch(bad_alloc err) {
cout << "配置失敗"
<< err.what()
<< endl;
return 1;
}
cout << "配置OK\n";
} while(ptr);
return 0;
}
沒有留言:
張貼留言