2012-08-13 11 views

risposta

87
std::exception <exception> interface (debatable if you should catch this) 
    std::bad_alloc <new> failure to allocate storage 
     std::bad_array_new_length <new> invalid array length 
    std::bad_cast <typeinfo> execution of an invalid dynamic-cast 
    std::bad_exception <exception> signifies an incorrect exception was thrown 
    std::bad_function_call <functional> thrown by "null" std::function 
    std::bad_typeid <typeinfo> using typeinfo on a null pointer 
    std::bad_weak_ptr <memory> constructing a shared_ptr from a bad weak_ptr 
    std::logic_error <stdexcept> errors detectable before the program executes 
     std::domain_error <stdexcept> parameter outside the valid range 
     std::future_error <future> violated a std::promise/std::future condition 
     std::invalid_argument <stdexcept> invalid argument 
     std::length_error <stdexcept> length exceeds its maximum allowable size 
     std::out_of_range <stdexcept> argument value not in its expected range 
    std::runtime_error <stdexcept> errors detectable when the program executes 
     std::overflow_error <stdexcept> arithmetic overflow error. 
     std::underflow_error <stdexcept> arithmetic underflow error. 
     std::range_error <stdexcept> range errors in internal computations 
     std::regex_error <regex> errors from the regular expression library. 
     std::system_error <system_error> from operating system or other C API 
      std::ios_base::failure <ios> Input or output error 

Fonte: http://en.cppreference.com/w/cpp/error/exception
In pratica, la maggior parte delle eccezioni sono eccezioni personalizzate derivate da logic_error e runtime_error. Non che questi siano trascurati, ma che molte eccezioni sono specifiche del dominio.

Ricordare che un'eccezione dovrebbe riflettere cosa è andato storto e non chi lo ha lanciato. (No "MyProgramException" s)

+0

'bad_function_call, domain_error, e future_error' su MSDN sono peggiori exampled e ha spiegato :( –

+0

' bad_function_call' viene generata quando si dispone di un oggetto std :: function costruito di default e si tenta di chiamare la funzione che esegue il wrapping .Quando non esiste una funzione wrapping, non c'è niente da chiamare. –

+1

'bad_function_call' viene generato quando si tenta di richiamare' std :: function 'Questo non è pronto (aka, const predefinito guidato o esplicitamente cancellato via nullptr). 'future_error' viene usato quando si viola una delle molte precondizioni delle funzioni per' promise' e ​​'future'. E 'domain_error' è (in teoria) per i casi in cui l'input di una funzione è al di fuori dell'intervallo valido per quella funzione (come un numero negativo per' std :: sqrt'). –

46

Vedere questo site

enter image description here

Exception    Description 
=================================== 
std::exception   An exception and parent class of all the standard C++ exceptions. 
std::bad_alloc   This can be thrown by new. 
std::bad_cast   This can be thrown by dynamic_cast. 
std::bad_exception  This is useful device to handle unexpected exceptions in a C++ program 
std::bad_typeid   This can be thrown by typeid. 
std::logic_error  An exception that theoretically can be detected by reading the code. 
std::domain_error  This is an exception thrown when a mathematically invalid domain is used 
std::invalid_argument This is thrown due to invalid arguments. 
std::length_error  This is thrown when a too big std::string is created 
std::out_of_range  This can be thrown by the at method from for example a std::vector and std::bitset<>::operator[](). 
std::runtime_error  An exception that theoretically can not be detected by reading the code. 
std::overflow_error  This is thrown if a mathematical overflow occurs. 
std::range_error  This is occured when you try to store a value which is out of range. 
std::underflow_error This is thrown if a mathematical underflow occurs. 
+0

Questo è buono, ma manca le eccezioni C++ 11 e non mostra quali sono le eccezioni in quali intestazioni. –

+2

@MooingDuck La tua domanda è stata taggata 'C++', non 'C++ 11', e tutti risiedono nello stesso' ' – TemplateRex

+5

[C++ significa qualunque sia l'ultima versione, mentre C++ 11 e C++ 03 sono domande su quelle specifiche versioni] (http://meta.stackexchange.com/questions/112641/when-did-the-c-tag-start-to-imply-c11-by-default). la mia domanda non riguarda una versione specifica, ma solo le informazioni più aggiornate su C++. Ad ogni modo, modificherò la domanda per citare C++ 11. Inoltre, non tutti questi errori sono in "" come mostrato da http://ideone.com/uqM6h –

Problemi correlati