草庐IT

c++ - 初始化列表中的 try/catch 是如何工作的?

我们认为可能会发生初始化异常。所以我们写了try/catchblock。intf(){throw1;}classA{public:A()try:_k(f()){}catch(int){std::cout但是catch会在更深一层上重新抛出异常。这意味着下一个代码try{Aa;}catch(int){std::cout将输出:Exception1Exception2为什么这个try/catchblock的行为与普通的try/catchblock不同?完整代码示例:http://ideone.com/XjY2d 最佳答案 您的问题似乎是

c++ - 初始化列表中的 try/catch 是如何工作的?

我们认为可能会发生初始化异常。所以我们写了try/catchblock。intf(){throw1;}classA{public:A()try:_k(f()){}catch(int){std::cout但是catch会在更深一层上重新抛出异常。这意味着下一个代码try{Aa;}catch(int){std::cout将输出:Exception1Exception2为什么这个try/catchblock的行为与普通的try/catchblock不同?完整代码示例:http://ideone.com/XjY2d 最佳答案 您的问题似乎是

c++ - 重新抛出异常

为什么以下不处理被重新抛出的异常?我尝试了所有组合,但没有一个会显示最后一次捕获的输出,所以我很困惑!DerivedD;try{throwD;}catch(constDerived&d){throw;}catch(constBase&b){coutDerivedD;try{throwD;}catch(constDerivedd){throw;}catch(constBaseb){coutDerivedD;try{throwD;}catch(constDerivedd){throw;}catch(constBase&b){coutDerivedD;try{throwD;}catch(co

c++ - 重新抛出异常

为什么以下不处理被重新抛出的异常?我尝试了所有组合,但没有一个会显示最后一次捕获的输出,所以我很困惑!DerivedD;try{throwD;}catch(constDerived&d){throw;}catch(constBase&b){coutDerivedD;try{throwD;}catch(constDerivedd){throw;}catch(constBaseb){coutDerivedD;try{throwD;}catch(constDerivedd){throw;}catch(constBase&b){coutDerivedD;try{throwD;}catch(co

c++ - 异常处理中的代码重用

我正在为一些用C++编写的功能开发Capi,我想确保不会从任何导出的C函数中传播异常。简单的方法是确保每个导出的函数都包含在:try{//Dotheactualcode}catch(...){returnERROR_UNHANDLED_EXCEPTION;}假设我知道C++代码中经常遗漏的一个异常是std::bad_alloc,我想特别对待它,我会写这样的东西:try{//Runtheactualcode}catch(std::bad_alloc&e){returnERROR_BAD_ALLOC;}catch(...){returnERROR_UNHANDLED_EXCEPTION;}

c++ - 异常处理中的代码重用

我正在为一些用C++编写的功能开发Capi,我想确保不会从任何导出的C函数中传播异常。简单的方法是确保每个导出的函数都包含在:try{//Dotheactualcode}catch(...){returnERROR_UNHANDLED_EXCEPTION;}假设我知道C++代码中经常遗漏的一个异常是std::bad_alloc,我想特别对待它,我会写这样的东西:try{//Runtheactualcode}catch(std::bad_alloc&e){returnERROR_BAD_ALLOC;}catch(...){returnERROR_UNHANDLED_EXCEPTION;}

c++ - 函数 try catch 语法和 main

一个鲜为人知但几乎从未使用过的C++特性被声明:voidfoo();一种可能的法律定义是:voidfoo()try{throw42;}catch(...){}这里是wholefunctionimplementationwrappediswithinatry/catchpair,这似乎类似于允许this.intmain()这样做合法吗?例如:intmain()try{throw42;}catch(...){}Therulesformain,n3290§3.6.1主要讨论它应该采用什么参数以及它返回什么-他们似乎并没有像处理其他各种奇怪的事情(例如链接)那样明确禁止它,您可能会尝试尝试。这

c++ - 函数 try catch 语法和 main

一个鲜为人知但几乎从未使用过的C++特性被声明:voidfoo();一种可能的法律定义是:voidfoo()try{throw42;}catch(...){}这里是wholefunctionimplementationwrappediswithinatry/catchpair,这似乎类似于允许this.intmain()这样做合法吗?例如:intmain()try{throw42;}catch(...){}Therulesformain,n3290§3.6.1主要讨论它应该采用什么参数以及它返回什么-他们似乎并没有像处理其他各种奇怪的事情(例如链接)那样明确禁止它,您可能会尝试尝试。这

c++ - 在 C++ 中重用异常处理代码

我有这两个函数,有重复的异常处理,唯一的目的就是显示错误信息:voidfunc1()noexcept{try{do_task();do_another_task();}catch(conststd::out_of_range&e){show_msg("Outofrangeerror",e.what());}catch(conststd::logic_error&e){show_msg("Logicerror",e.what());}catch(conststd::system_error&e){show_msg("Systemerror",e.what());}catch(consts

c++ - 在 C++ 中重用异常处理代码

我有这两个函数,有重复的异常处理,唯一的目的就是显示错误信息:voidfunc1()noexcept{try{do_task();do_another_task();}catch(conststd::out_of_range&e){show_msg("Outofrangeerror",e.what());}catch(conststd::logic_error&e){show_msg("Logicerror",e.what());}catch(conststd::system_error&e){show_msg("Systemerror",e.what());}catch(consts