Re: [問題] try catch(...)的問題
基本上我覺得書上都會有答案...
※ 引述《QQ29 (我愛阿蓉)》之銘言:
: try
: {
: 1. throw; //跑到這行就掛了?
不指定 exception object 的意思是在 catch block 中重丟 exception
比如說:
try{
...
}catch(...){
Log("Some exception happened!");
throw;
}
這邊的 throw 會把同一個 exception 物件繼續往上一層丟
而你沒弄懂它是拿來 rethrow 而把它放在 catch block 以外的地方
依照 C++ 標準 15.1p8:
If no exception is presently being handled, executing a throw-
expression with no operand calls terminate() (15.5.1).
: 2. throw exception(); //會進到catch
: 3. int a, b = 0; a= 1/b; //catch不到....
: 4. int *ptr = 0; *ptr = 5566; //catch 不到
: }
: 我比較想了解 為啥 1,3,4都catch不到 1 和 2 有什麼差異嗎?
一併解釋 3 和 4
原因很簡單就是 C++ 重視執行效率
所以規定 3 和 4 是 undefined behavior
如果對這兩種情況都需要拋出 exception
那麼程式碼的執行效率會大幅降低
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.3.139
推
10/28 16:32, , 1F
10/28 16:32, 1F
→
10/28 16:32, , 2F
10/28 16:32, 2F
→
10/28 16:33, , 3F
10/28 16:33, 3F
→
10/28 16:33, , 4F
10/28 16:33, 4F
→
10/28 16:34, , 5F
10/28 16:34, 5F
→
10/28 16:34, , 6F
10/28 16:34, 6F
→
10/28 16:35, , 7F
10/28 16:35, 7F
→
10/28 16:35, , 8F
10/28 16:35, 8F
推
10/28 16:46, , 9F
10/28 16:46, 9F
→
10/28 16:59, , 10F
10/28 16:59, 10F
→
10/28 17:00, , 11F
10/28 17:00, 11F
→
10/28 17:00, , 12F
10/28 17:00, 12F
→
10/28 17:01, , 13F
10/28 17:01, 13F
推
10/28 23:09, , 14F
10/28 23:09, 14F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 5 篇):