c++ try...catch异常处理
生活随笔
收集整理的這篇文章主要介紹了
c++ try...catch异常处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ?c++可以通過throw語句和try...catch語句實現對異常的處理。
#include <iostream>using namespace std;int main() {double m ,n;cin >> m >> n;try {if( n == 0)throw 1; //拋出int類型異常elsecout << m / n << endl;}catch(double d) {cout << "catch(double) " << d << endl;}catch(int e) {cout << "catch(int) " << e << endl;}catch (...) {cout << "catch (...)" << endl;}cout << "finished" << endl;return 0; }編譯運行,當輸入除數為0時就會拋出異常。
4 0
catch(int) 1
finished
如將throw 1換成throw 0.1就會走到catch(double d)對應的分支,換成throw "12"就會走到catch (...)對應的分支。
?
參考資料:http://c.biancheng.net/view/422.html
總結
以上是生活随笔為你收集整理的c++ try...catch异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mutable关键字
- 下一篇: c++ 互斥量和条件变量