C + + Exception Second---C + + standard Library Exception class exception use __c++

Source: Internet
Author: User
Tags throw exception
1 Inheritance Diagram


2 Specific explanations

C + + Standard library Exception class inheritance hierarchy in the root class is exception, which is defined in the exception header file, it is the C + + standard library all functions throw exception of the base class, exception interface definition is as follows:

Namespace Std {

Class Exception {

Public

Exception () throw (); Do not throw any exceptions

Exception (const exception& e) throw ();

exception& operator= (const exception& e) throw ();

Virtual ~exception () throw) ();

Virtual Const char* What () const throw (); Returns a description of the exception

};

}

In addition to the exception class, C + + provides classes for reporting abnormal programs, and in the error models reflected in these predefined classes, there are two broad categories of logic errors and run-time errors.

Logic errors mainly include Invalid_argument, Out_of_range, Length_error, Domain_error. When a function receives an invalid argument, it throws a Invaild_argument exception, and if the function receives an argument that is outside 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);

};

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

};

}

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 a range error in an arithmetic operation by throwing a range_eroor, and reporting an overflow error by throwing a 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, and exception is also a Bad_alloc base class for reporting cases where 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.


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


3 Examples

#include <stdexcept>


Try
  {
  throw invalid_argument ("hah");
 //throw out_of_range ("We get the Outofrange err or ");
   }catch (invalid_argument& in)
  {
  cout<< "We catch Invalid_argument" <& Lt;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.