代碼:
#include <iostream>
#include <stdexcept>
using namespace std;
void f3();
int main()
{
void f1();
try
{
f1();
}
catch(char)
{
cout<<"main"<<endl;
}
return 0;
}
void f1()
{
catch(char)
{
cout<<"f1"<<endl;
}
try
{
f3();
}
catch(double)
{
cout<<"f1"<<endl;
}
}
void f3()
{
char a;
try
{
throw a;
}
catch(int)
{
cout<<"int"<<endl;
}
}
代碼意圖:
測試throw和catch查錯的原理,想看一下如果catch在try前面的時候系統可不可以查到錯誤
運行結果:
直接編譯,結果如下圖。
改錯:
將f1()函數中catch放到try下面後,運行結果爲輸出f1,表明在f1函數中找到匹配的錯誤。
學習小結:
問題很小,在找出來時考慮有沒有必要發出來
但是最後還是發出來了,不積硅步無以至千里吧。
加油咯,博客發的太少了,纔有這麼多的顧慮...