嘗試
關鍵字 try
後跟一個塊,或者是建構函式初始化列表,然後是一個塊(參見此處 )。try 塊後跟一個或多個 catch 塊 。如果異常傳播出 try 塊,則 try 塊之後的每個相應 catch 塊都有機會處理異常(如果型別匹配)。
std::vector<int> v(N); // if an exception is thrown here,
// it will not be caught by the following catch block
try {
std::vector<int> v(N); // if an exception is thrown here,
// it will be caught by the following catch block
// do something with v
} catch (const std::bad_alloc&) {
// handle bad_alloc exceptions from the try block
}