尝试

关键字 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
}