2012-08-13 16 views
78

में कौन सी अपवाद कक्षाएं मानक सी ++ लाइब्रेरी में शामिल अपवाद वर्ग हैं, और इनका उपयोग किसके लिए किया जाना चाहिए? मुझे पता है कि कुछ नए सी ++ 11 अपवाद हैं, लेकिन मुझे यकीन नहीं है कि वे क्या हैं या वे कहां हैं।मानक सी ++ लाइब्रेरी

उत्तर

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 

स्रोत: http://en.cppreference.com/w/cpp/error/exception
अभ्यास में, सबसे अपवाद हैं कस्टम अपवाद logic_error और runtime_error से ली गई। ऐसा नहीं है कि इन्हें उपेक्षित किया गया है, लेकिन कई अपवाद डोमेन विशिष्ट हैं।

ध्यान रखें कि एक अपवाद को प्रतिबिंबित करना चाहिए कि क्या गलत हुआ और ने इसे फेंक दिया। (कोई "MyProgramException" रों)

+0

'bad_function_call, domain_error और MSDN पर future_error' वे सबसे खराब exampled कर रहे हैं और विस्तार से बताया :( –

+0

' bad_function_call' फेंक दिया जाता है जब आपके पास एक डिफ़ॉल्ट-निर्मित std :: फ़ंक्शन ऑब्जेक्ट है और आप जिस फ़ंक्शन को लपेटते हैं उसे कॉल करने का प्रयास करते हैं। चूंकि कोई लपेटा हुआ फ़ंक्शन नहीं है, इसलिए कॉल करने के लिए कुछ भी नहीं है। –

+1

'bad_function_call' फेंक दिया जाता है जब आप 'std :: function 'वह तैयार नहीं है (उर्फ, डिफ़ॉल्ट कॉन्स nullptr के माध्यम से ructed या स्पष्ट रूप से मंजूरी दे दी)। 'future_error' का उपयोग तब किया जाता है जब आप 'वादे' और 'भविष्य' के कार्यों के कई पूर्व शर्तों में से एक का उल्लंघन करते हैं। और 'डोमेन_error' उन मामलों के लिए है (सिद्धांत में) जहां फ़ंक्शन में इनपुट उस फ़ंक्शन के लिए मान्य सीमा से बाहर है (जैसे' std :: sqrt' के लिए ऋणात्मक संख्या)। –

46

देखें इस 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

यह अच्छा है, लेकिन सी ++ 11 अपवाद गायब है, और यह नहीं दिखाता है कि कौन से अपवाद हैंडर्स हैं। –

+2

@MooingDuck आपका प्रश्न 'C++' टैग किया गया था, 'C++ 11' नहीं, और वे सभी एक ही' ' – TemplateRex

+5

[सी ++ का अर्थ है जो नवीनतम संस्करण है, जबकि सी ++ 11 और सी ++ 03 हैं उन विशिष्ट संस्करणों के बारे में प्रश्न] (http://meta.stackexchange.com/questions/112641/when-did-the-c-tag-start-to-imply-c11-by-default)। मेरा प्रश्न एक विशिष्ट संस्करण के बारे में नहीं है, केवल सी ++ पर सबसे अद्यतित जानकारी है। किसी भी तरह से, मैं सी ++ 11 का उल्लेख करने के लिए प्रश्न संपादित कर दूंगा। साथ ही, उन सभी त्रुटियों को ' 'में दिखाया गया है जैसा कि http://ideone.com/uqM6h –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^