C ++ exception Article 2-Use of C ++ standard library exception classes

Source: Internet
Author: User
1 Inheritance Graph

2. Details

The root class in the C ++ standard library exception class hierarchy is exception, which is defined in the exception header file. It is the base class for all functions in the C ++ standard library to throw exceptions, the exception interface is defined as follows:

Namespace STD {

Class exception {

Public:

Exception () Throw (); // No exception is thrown

Exception (const exception & E) Throw ();

Exception & operator = (const exception & E) Throw ();

Virtual ~ Exception () Throw )();

Virtual const char * What () const throw (); // return the exception description

};

}

In addition to the exception class, C ++ also provides some classes for reporting Abnormal Program conditions. In the error models reflected in these predefined classes, it mainly includes two categories: logical errors and runtime errors.

Logical errors include invalid_argument, out_of_range, length_error, and domain_error. When the function receives an invalid real parameter, it will throw an invaild_argument exception. If the function receives a real parameter beyond the expected range, it will throw an 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 );

};

Class out_of_range: Public logic_error {

Public:

Explicit out_of_range (const string & what_arg );

};

Class length_error: Public logic_error {

Public:

Explicit length_error (const string & what_arg );

};

Class domain_error: Public logic_error {

Public:

Explicit domain_error (const string & what_arg );

};

}

Runtime errors are caused by events outside the program domain. They can only be detected at runtime, including range_error, overflow_error, and underflow_error. The function reports a Range Error in arithmetic operations by throwing range_eroor and an overflow error by throwing overflow_error.

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

};

Class overflow_error: Public runtime_error {

Public:

Explicit overflow_error (const string & what_arg );

};

Class underflow_error: Public runtime_error {

Public:

Explicit underflow_error (const string & what_arg );

};

}

In addition, the bad_alloc exception is defined in the new header file. exception is also the base class of bad_alloc, which is used to report the situation where the new operator cannot properly allocate memory. When dynamic_cast fails, the program will throw the bad_cast exception class, which also inherits from the exception class.

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

3. Example

# Include <stdexcept>

Try
{
Throw invalid_argument ("hah ");
// Throw out_of_range ("we get the outofrange error ");
} Catch (invalid_argument & in)
{
Cout <"we catch invalid_argument" <in. What () <Endl;
} Catch (...)
{
Cout <"we catch Unexpected error, exit" <Endl;
Return 1;
}

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.