C + + Standard Library Exception class

Source: Internet
Author: User
Tags mathematical functions

Transferred from: http://blog.csdn.net/zhq651/article/details/8425579

exception

The base class in the C + + standard library Exception class inheritance hierarchy is exception, which is defined in the exception header file, which is the base class in which all functions of the C + + standard library throw exceptions, and the interface definition for exception is as follows:

Namespace STD {       class Exception {public       :             exception () throw ();   Do not throw any exception             exception (const exception& e) throw ();             exception& operator= (const exception& e) throw ();             Virtual ~exception () throw ();             Virtual Const char* What () const throw (); Returns the description information of the exception       };

In addition to the exception class, C + + provides a number of derived classes for reporting cases where the program is unhealthy, and in the error model that is reflected in these predefined classes, there are two main categories of logic errors and run-time errors.

Logic error: Logic_error

Logic errors mainly include Invalid_argument, Out_of_range, Length_error, Domain_error.

A Invaild_argument exception is thrown when the function receives an invalid argument, and if the function receives an argument that exceeds the expected range, it throws a Out_of_range exception, and so on.

Namespace Std {         class Logic_error:public exception {public         :               explicit Logic_error (const string &what_ arg);         };         Class Invalid_argument:public Logic_error {public         :               explicit invalid_argument (const string &what_arg);// illegal arguments     };         Class Out_of_range:public Logic_error {public         :               explicit Out_of_range (const string &what_arg);//member Out of bounds     };         Class Length_error:public Logic_error {public         :               explicit Length_error (const string &what_arg);//Object Very long c15/>};         Class Domain_error:public Logic_error {public         :               explicit Domain_error (const string &what_arg);//Domain Error c19/>};}
Run-time error: Runtime_error

run-time errors are raised by events outside the program domain and can only be detected at run time, mainly including Range_error, Overflow_error, Underflow_error. A function can report an overflow error by throwing a overflow_error by throwing Range_error to report a range error in arithmetic operations.

Namespace Std {         class Runtime_error:public exception {public         :               explicit Runtime_error (const string & WHAT_ARG);         };         Class Range_error:public Runtime_error {public         :               explicit Range_error (const string &what_arg);//arithmetic Operation range Error         };         Class Overflow_error:public Runtime_error {public         :               explicit Overflow_error (const string &what_arg);// Overflow error         };         Class Underflow_error:public Runtime_error {public         :               explicit Underflow_error (const string &what_arg); Underflow error         };}

Note: Domain and range errors is both used when dealing with mathematical functions.

C + + built-in exception classes

In addition, the BAD_ALLOC exception is defined in the new header file, and exception is the base class for Bad_alloc, which reports that the new operator does not properly allocate memory. When dynamic_cast fails, the program throws the Bad_cast exception class, which also inherits from the exception class.

C + + Standard Library Exception class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.