正常代码放在try块,catch中捕获try块用throw关键字抛出的异常。
例如:
try { //将可能出现异常的情况放到try块
if ( a >12 ) throw 12; // a >12时抛出,若成立下面代码就不会再执行了
if ( a < 10 ) throw error;
…
throw xxx ; //其他异常情况抛出
}
catch ( int result ) { //由于上面抛出的是整数,所以用int
cout << “result is:” << result << endl;
b = result + 1;
}
catch (char * result ) { //若上面抛出的是字符,所以用char
cout << “result is:” << result << endl;
b = result + 1;
}
…