2016-07-04 36 views
0

在開發的C面對的問題++,其中catch塊未顯示所需的錯誤信息try catch塊顯示錯誤的錯誤消息

#include<iostream> 
using namespace std; 
void mightGoWrong() 
{ 
    bool error1; 
    bool error2; 
    if(error1) 
    { 
    throw "Issue encountered!!"; 
    }  
} 
int main(void) 
{ 
    try 
    { 
    mightGoWrong(); 
    } 
    catch(int e) 
    { 
    cout << "Error Code is: "<<e<<endl; 
    } 
    cout<<"Still running"<<endl; 

} 

該消息得到的是:仍然Running.Need知道我在做什麼錯誤

+0

如果聲明'bool error1 = true;'會發生什麼? –

回答

0

在代碼開始工作之前,您需要初始化您的布爾變量。

總是給你的變量一些默認值被認爲是最佳做法。您的error1值沒有分配給它的值,因此拋出錯誤永遠無法完成它的工作。

另外,你正在拋出一個字符串,所以你需要捕獲一個字符串不是int。

void mightGoWrong() 
{ 
    bool error1=true; 
    bool error2=true; 
    if(error1) 
    { 
     throw "Issue encountered!!"; 
    }  
} 
int main(void) 
{ 
    try 
    { 
    mightGoWrong(); 
    } 
    catch(string e) 
    { 
    cout << "Error Code is: "<<e<<endl; 
    } 
    cout<<"Still running"<<endl; 

}