2012-07-27 47 views
0

我有以下class exception正確的方式抓創建例外

namespace A{ 
namespace B{ 
    namespace C{ 
    class internal_error : public std::runtime_error{ 
     public: 
      explicit internal_error(const std::string &arg) : std::runtime_error(arg) {}; 
    }; 
    } 
    } 
    } 

而且在我的代碼有:

try{ 
    if(mkdipath(dirname, DIR_CREATION_MODE, &err)){ 
     string msg; 
     msg = "failed to create path " + *dirname; 
     logmsg(MSERROR, msg.c_str(), who); 
     throw A::B::C::internal_error(msg); 
    } 
} 
catch(){ 
    // how am I going to catch a A::B::C::internal_error? 
} 

我的問題是:我應該如何去趕上答: :B ::ç:: INTERNAL_ERROR? 我應該使用:

catch(A::B::C::internal_error &error){ 
     string msg("You should never had happened\n"); 
     logmsg(MSERROR, msg.c_str(), who); 
} 

請忽略標籤MSERRORwhomkdirpath ......他們不是這個問題很重要。

+0

此代碼無效嗎? – robert 2012-07-27 13:17:04

+0

刪除internal_error之前的_,所以它成爲A :: B :: C :: internal_error – fbafelipe 2012-07-27 13:17:44

+0

我刪除了......我的問題是:我是否正確地做到了? – cybertextron 2012-07-27 13:18:23

回答

1
catch(A::B::C::internal_error &error){ 
    //... 
} 

很好,請注意確定前導下劃線來自哪裏,可能是一個錯字。

+0

以及如何處理該錯誤? – cybertextron 2012-07-27 13:22:05

+0

@philippe我應該怎麼知道?也許只是像你做的那樣記錄它就足夠了,也許你會想要關閉應用程序,或者嘗試不同的路徑。 – 2012-07-27 13:31:58

0
try{ 
// ... 
} catch (const A::internal_error& ex) { 
// ... 
} catch (const B::internal_error& ex) { 
// ... 
} catch (const C::internal_error& ex) { 
// ... 
} 

這是你在找什麼?

+0

即使您可以使用字符串參數作爲例外。如:std :: string&ex){ – 2012-07-27 13:22:59