相关疑难解决方法(0)

是否在嵌套的"尝试"中重新抛出异常合法?

以下是否在C++中定义良好?我被迫"转换"异常返回代码(有问题的API被许多C用户使用,所以我需要确保在控制返回给调用者之前捕获并处理所有C++异常).

enum ErrorCode {…};
ErrorCode dispatcher() {
   try {
      throw;
   }
   catch (std::bad_alloc&) {
      return ErrorCode_OutOfMemory;
   }
   catch (std::logic_error&) {
      return ErrorCode_LogicError;
   }
   catch (myownstdexcderivedclass&) {
      return ErrorCode_42;
   }
   catch(...) {
      return ErrorCode_UnknownWeWillAllDie;
   }
}

ErrorCode apifunc() {
   try {
      // foo() might throw anything
      foo();
   }
   catch(...) {
      // dispatcher rethrows the exception and does fine-grained handling
      return dispatcher();
   }
   return ErrorCode_Fine;
}

ErrorCode apifunc2() {
   try {
      // bar() might throw anything
      bar();
   }
   catch(...) {
      return dispatcher(); …
Run Code Online (Sandbox Code Playgroud)

c++ exception-handling

22
推荐指数
1
解决办法
1491
查看次数

标签 统计

c++ ×1

exception-handling ×1