The basic idea of C + + exception handling

Source: Internet
Author: User

Traditional error handling mechanism
The error is handled by the function return value.

The basic idea of exception handling

1) The exception handling mechanism of C + + makes exception's throwing and exception handling unnecessary in the same function, so that the underlying function can focus on solving the specific problem without too much consideration of exception handling. Upper-level callers can then design the handling of different types of exceptions in the appropriate location.
2) exception is specifically for the abstract programming of a series of error handling, C + + can not rely on the function mechanism, because the nature of the stack structure is advanced, and then access, can not jump, but the characteristics of error handling is to encounter the error message you want to go to a number of levels to retry,

3) The anomaly is detached from the function mechanism, which determines the leap-back of the function.
4) Exception spanning function

Exception Basic Syntax


1) If an exception is thrown, an exception object is created and thrown by the throw operation.
2) Embed the program segments that may throw exceptions in the try block. Control reaches the try statement through normal sequential execution, and then executes the protection segment within the try block.
3) If no exception is raised during the protection segment execution, then the catch clause following the try block is not executed. The program proceeds from the statement following the last catch clause following the try block.
4) The catch clause is checked in the order in which it appears after the try block. The matching catch clause will catch and handle the exception (or continue throwing the exception).
5) If the matching processor is not found, the run function terminate will be called automatically, and its default function is to call abort to terminate the program.
6) cannot handle the exception, can be in the last branch of the catch, using the throw syntax, throw up
7) Exception mechanism and function mechanism do not interfere with each other, but the way of capturing is based on type matching. Snapping matches the function return type rather than matching the function parameters, so snapping does not take into account the multiple data type matching issues in a throw.
The catch code block must appear after the try, and multiple catch code blocks can appear after the try block to catch a variety of different types of throws.
Exception mechanism is based on the principle that the program is essentially a data entity in doing some operations, so the occurrence of anomalies, it must be an entity has gone wrong, the entity corresponding to the data type as the basis for throwing and capturing.
8) exception catching is strictly matched by type
The severity of the type matching of an exception capture can be comparable to the type of template, which does not allow implicit conversions of compatible types, for example, the throw char type is not captured with the int type. For example, the following code does not output "int exception." And thus does not output "that's OK." Prompt to exit because an exception occurred

int main () {try{throw ' H ';} catch (int) {cout << "int exception.\n";} cout << "That ' s ok.\n"; return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The basic idea of 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.