2012-07-27 6 views
0

Je les suivantes class exception:manière correcte pour attraper une exception créée

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) {}; 
    }; 
    } 
    } 
    } 

Et dans mon code que j'ai:

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? 
} 

Ma question est: comment vais-je attraper A: : B :: C :: internal_error? Dois-je utiliser:

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

S'il vous plaît ignorer les balises MSERROR, who, mkdirpath ... ils ne sont pas importants à la question.

+0

Est-ce que ce code ne fonctionne pas? – robert

+0

Supprimer le _ avant internal_error, de sorte qu'il devient A :: B :: C :: internal_error – fbafelipe

+0

J'ai supprimé ... ma question est: est-ce que je le fais correctement? – cybertextron

Répondre

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

est bien, notez bien d'où vient le trait de soulignement principal, probablement une faute de frappe.

+0

et comment puis-je gérer cette erreur? – cybertextron

+0

@philippe comment devrais-je savoir? Il suffit peut-être de simplement l'enregistrer comme vous l'avez fait, peut-être que vous voulez fermer l'application, ou réessayer avec un chemin différent. –

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

Est-ce ce que vous cherchez?

+0

Même vous pouvez utiliser un argument de chaîne comme exception. comme: std :: string & ex) { –

Questions connexes