C + + Note (8): C + + exception handling

Source: Internet
Author: User
Tags throw exception

Exception handling

The C + + exception handling mechanism is a very powerful and flexible tool for effectively handling run errors, which provides more flexibility, security, and robustness, overcoming the problems of traditional methods.
Exceptions are thrown and handled using the following three keywords: try, throw, catch.
Try Statement
The try block consists of the keyword try and the following pair of curly braces, the code block defined in the curly braces, which is the code that can appear the exception. The try code block contains statements that can cause exceptions and statements that should be skipped when an exception occurs. (that is, the function that can cause the exception and the statement that should be executed if no exception occurs). The exception may be caused directly by code in the try code block, possibly due to calls to other functions, or by deep nested function calls initiated by code in the try code block.
Catch statement
The exception is handled by the catch processor, and he catches and handles the exception. After each try code block, you must be at least a catch processor next to it. Each catch processor starts with a keyword catch and specifies an exception parameter (exception parameter) in a pair of parentheses, indicating the type of exception (DivideByZeroException) it can handle. When the try block exception occurs, it executes the exception parameter whose type matches the exception of the catch handler (that is, the type of the catch block corresponds exactly to the type of exception being thrown, or his base class). If the exception parameter contains an optional parameter name, the catch processor can use this parameter name to interact with the exception caught by the catch processor code weight.
Throw statement
The throw statement is used to actively throw exceptions. If an exception is found in the program segment of the TRY statement block, including the function in which it was called, and the exception is thrown, the exception can be caught and processed by a catch statement after the try statement block, and the condition that is caught and processed is that the type of the thrown exception matches the exception type of the catch statement. Because C + + uses data types to differentiate between different exceptions, the value of the expression in the throw statement has no practical significance when judging the exception, and the type of the expression is particularly important.

The general format for exception handling is:
   Try
{
Checked statements
     throw exception;
}
   catch (Exception type 1)
{
Statement for exception handling 1
}
   catch (Exception type 2)
{
Statement for exception Handling 2
}
catch (...) Catch all the exceptions
{
}
when an exception is matched, only the type is matched and the value of the exception object thrown is not concerned.

Type of exception
(1) You can throw a basic data type exception,such as int and char, etc.
(2) Can throw complex data type exceptions,such as structs (structs are also classes in C + +) and classes;
(3)exception Handling for C + + must be actively checked by the caller. Once an exception is thrown,If the program does not capture, it will cause the program to terminate abnormally.
 
Precautions
(1) in the try and catch blocks must be enclosed in curly braces, even if there is only one statement within the curly braces can not omit the curly braces;
(2) Try and catch must appear in pairs, a try_catch result can only have one try block, but can have more than one catch block, in order to match with different exception information;
(3) If you do not specify the type of exception information in the catch block, and use ellipsis "...", it can catch any type of exception information

(4) C + + Once an exception is thrown, if the program does not have any capture, then the system will automatically call a system function terminate, called by it to abort the program
(5) The constructor can throw an exception inside the destructor do not throw an exception.
 
The type of the standard exception object
in the C + + standard library, some functions throw specific exceptions, while others do not throw any exceptions at all.
because there is no explicit provision in the C + + standard, library functions for C + + can throw any object or do not throw.

However, the implementation of the C + + standard Recommendation runtime reports an error by throwing an exception type that is defined in <stdexecpt> or its derived type:
Class Logic_error;
Class Domain_error;
Class Invalid_argument;
Class Length_error;
Class Out_of_range;
Class Runtime_error;
Class Range_error;
Class Overflow_error;
Class Underflow_error;

C + + Note (8): C + + exception handling

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.