2016-04-21 11 views
5

Nel seguente codice, voglio ottenere il messaggio what() di un boost :: exception.Ottieni il messaggio what() di boost :: exception

#include <iostream> 
#include <boost/lexical_cast.hpp> 
#include <boost/exception/diagnostic_information.hpp> 

int main(void) 
{ 
    try 
    { 
     int i(boost::lexical_cast<int>("42X")); 
    } 
    catch (boost::exception const &e) 
    { 
     std::cout << "Exception: " << boost::diagnostic_information_what(e) << "\n"; 
    } 
    return 0; 
} 

quando l'eseguo, ottengo il messaggio:

Exception: Throw location unknown (consider using BOOST_THROW_EXCEPTION) 
Dynamic exception type: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> > 

Ma quando io non intercettare l'eccezione, le uscite di shell:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >' 
    what(): bad lexical cast: source type value could not be interpreted as target 
[1] 8744 abort  ./a.out 

voglio che un messaggio: bad lexical cast: source type value could not be interpreted as target ; ma non sono riuscito a trovare il modo di averlo Il sistema di eccezioni boost è un mistero per me.

Come ottenere questo stesso messaggio?

Modifica: boost :: l'eccezione non ha il metodo what(). Quindi, come può scrivere la shell std::exception::what: bad lexical cast: source type value could not be interpreted as target, poiché questo non è un std::exception?

+0

dopo l'avvento di C++ 11 di eccezioni nidificate non riesco a trovare una forte necessità di usare il boost ::eccezione. è troppo tardi per farlo nel modo standard? http://en.cppreference.com/w/cpp/error/throw_with_nested –

risposta

4

Cattura come bad_lexical_cast utilizzare il metodo what():

catch (const boost::bad_lexical_cast& e) 
{  // ^^^^^^^^^^^^^^^^^^^^^^^ 
    std::cout << "Exception: " << e.what() << "\n"; 
           // ^^^^^^^^ 
} 

e verrà visualizzato Exception: bad lexical cast: source type value could not be interpreted as target

+2

Grazie mille. Com'è possibile che la classe base non contenga un metodo virtuale puro 'what()'? Perché una tale scelta? – Boiethios

+0

eredita 'what()' da 'std :: bad_cast' come visto [qui] (http://www.boost.org/doc/libs/1_40_0/libs/conversion/lexical_cast.htm#bad_lexical_cas), che eredita da ['std :: exception'] (http://en.cppreference.com/w/cpp/types/bad_cast) –

+0

È strano. Supponevo che ogni eccezione derivasse dall'unico 'boost :: exception', come' std :: exception'. – Boiethios

4

Da the diagnostic_information_what reference:

La funzione diagnostic_information_what è destinata ad essere chiamato da uno std :: eccezione definita dall'utente :: cosa() override.

La funzione non è supposto per dare il messaggio dalla funzione what(), si suppone di essere utilizzato in la funzione what() per creare un messaggio di ritornare.

poi proseguire, da the boost::lexical_cast reference:

Se la conversione non riesce, un'eccezione bad_lexical_cast viene generata.

Così lascia dare un'occhiata alla bad_lexical_cast:

class bad_lexical_cast : public std::bad_cast 

Si eredita dalla standard distd::bad_cast che eredita da std::exception che hanno una funzione di what() membro.

Quindi la soluzione è catturare boost::bad_lexical_cast (o std::exception) anziché boost::exception che non è coinvolto affatto.

Problemi correlati