C + + exception handling basic idea __c++

Source: Internet
Author: User

Traditional error handling mechanism
Error is handled by function return value.

The basic idea of exception handling

1 The exception handling mechanism of C + + makes exception and exception handling not in the same function, so that the underlying function can focus on solving specific problems, without too much consideration of abnormal processing. Upper-level callers can then design the handling of different types of exceptions in a suitable location.
2 The exception is specifically directed against a series of errors in abstract programming, 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 encountered error message will want to go to a number of levels above to try again, as shown

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

Exception Basic Syntax


1 if there is an exception, the throw operation creates an exception object and throws it.
2 Embed the program segment that may throw an exception in a try block. Control arrives at the try statement in the normal order, and then executes the protection segment within the try block.
3 If no exception is caused during the protection segment execution, the catch clause following the try block is not executed. The statement that follows the last catch clause following the TRY block continues to execute.
4 The Catch clause is checked in the order in which it appears after the try block. A matching catch clause will catch and handle the exception (or continue throwing the exception).
5 If a matching processor is not found, the run function terminate is called automatically, and its default function is to invoke the abort termination program.
6 cannot handle the exception, can be in the last branch of catch, use throw syntax, throw up
7 The exception mechanism and function mechanism do not interfere with each other, but the way to capture is based on type matching. Capturing corresponds to a function return type rather than a matching function parameter, so capturing does not take into account a variety of data type matching issues in a throw.
A catch code block must appear after a try, and multiple catch blocks can appear after a try block to catch various types of throws.
The exception mechanism is based on the principle that the program operation is essentially a data entity doing some operations, so the occurrence of anomalies, it must be an entity out of error, the corresponding data type of the entity as the basis for throwing and capturing.
8) exception capture strictly according to the type match
The severity of the type matching of the exception captures can match the type of the template, and it does not allow implicit conversions of compatible types, for example, the cast 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" OK. "Prompts to exit after an exception has occurred

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

	return 0;
}
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: 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.