Use of try and throw and catch statements in C + + programming exception handling _c language

Source: Internet
Author: User
Tags exception handling modifiers volatile

To implement exception handling in C + +, you can use try, throw, and catch expressions.
First, use a try block to close one or more statements that might throw an exception. The
Throw expression signals that an exception condition (usually an error) has occurred in a try block. You can use any type of object as the operand of the throw expression. This object is typically used to communicate information about the error. In most cases, it is recommended that you use one of the derived classes defined in the Std::exception class or standard library. If the class is not appropriate, it is recommended that you derive your own exception class from Std::exception.
to handle the exceptions that might be thrown, implement one or more catch blocks immediately after the try block. Each catch block specifies the type of exception that it can handle. The
The following example displays a try block and its handlers. Suppose Getnetworkresource () obtains data over a network connection, and two exception types are user-defined classes derived from std::exception. Note that exceptions are caught by a const reference in a catch statement. We recommend that you throw an exception by value and catch it through a constant reference.

 MyData MD; try {//Code that could throw a exception MD = Getnetworkresource ();} cat CH (const networkioexception& e) {//Code that executes ' a exception of type//networkioexception is thrown I
n the try block//...//LOG error message in the exception object Cerr << e.what (); The catch (const mydataformatexception& e) {//Code that handles another exception type//... cerr << e.what (
);  }//The following syntax shows a throw expression MyData Getnetworkresource () {//... if (iosuccess = false) throw
 Networkioexception ("Unable to connect"); 
 .. if (readerror) throw mydataformatexception ("Format error");

// ...

The code after the TRY clause is a protected part of the code. The throw expression throws (that is, causes) an exception. The code block after the catch clause is an exception handler. If the type is compatible in the throw and catch expressions, the handler catches the thrown exception. For a list of rules that govern the matching of types in a catch block, see how catch blocks are calculated (C + +). If the catch statement specifies an ellipsis (...) rather than a type, the catch block handles each type of exception. When you compile with the/EHa option, exceptions can include C-structured exceptions and system-generated or application-generated asynchronous exceptions, such as memory protection, 0 divide and floating-point conflicts. Because catch blocks are processed in a programmatic order to find matching types, try not to use an ellipsis handler to handle the associated try block. Please use the catch (...) with caution. , prevent the program from continuing unless the catch block knows how to handle a specific exception that is caught. The catch (...) block is generally used to log errors and perform special cleanup before the program stops executing.
An throw expression without an operand will cause the exception that is currently being handled to be raised again. We recommend that you use this form when you re raising an exception because this preserves the polymorphic type information of the original exception. Such expressions should only be used in catch handlers or functions that are called from a catch handler. The exception object that is raised again is the original exception object, not the replica.

try {
 throw csomeotherexception ();
catch (...) {
 //Catch all exceptions–dangerous!!!
 Respond (perhaps only partially) to the exception, then
 //Re-throw to pass the exception to some other handler
    // ...


How Catch blocks are calculated (C + +)
Although it is generally recommended that you throw a type that derives from Std::exception, C + + enables you to throw any type of exception. C + + Exceptions can be caught by specifying a catch handler of the same type as the exception that is thrown, or by a handler that captures any type of exception.
If the type of the exception being thrown is a class and it has a base class (or Class), it can be caught by a handler that accepts the exception type and a reference to the base of the exception type. Note that when an exception is caught by a reference, it is bound to the exception object that is actually thrown, otherwise it will be a copy (roughly the same as the parameters of the function).
When an exception is thrown, the exception is caught by the following types of catch handlers:

    • Any type of handler can be accepted (using ellipsis syntax).
    • Accepts handlers of the same type as the exception object, because it is a replica, so the const and volatile modifiers are ignored.
    • A handler that accepts references to the same type as the exception object.
    • A handler that accepts a reference to a const or volatile form of the same type as the exception object.
    • A handler that accepts the base class of the same type as the exception object, because it is a replica, so the const and volatile modifiers are ignored. The catch handler for the base class must not be in front of the catch handler of the derived class.
    • A handler that accepts a reference to the base class of the same type as the exception object.
    • A handler that accepts a const or volatile form of a reference to the base class of the same type as the exception object.
    • Accepts a handler for a pointer that can be converted to a pointer object that is raised through a standard pointer conversion rule.

The order in which catch handlers appear is meaningful because handlers for a given try block are checked in the order in which they appear. For example, it is an error to place a handler for a base class in front of a handler for a derived class. After a matching catch handler is found, no subsequent handlers are checked. Therefore, the ellipsis catch handler must be the last handler for its try block. For example:

// ...
catch (...)
 //Handle exception here.
Error:the next two handlers are never examined.
catch (const char * str)
 cout << "caught exception:" << str << endl;
catch (Cexcptclass E)
 //Handle Cexcptclass exception here.

In this example, the ellipsis catch handler is the only handler that has been checked.

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: 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.