A simple understanding of the two error handling methods (return value, exception)

Source: Internet
Author: User

1. Two Methods for error handling: (1) using the return value; (2) stack operation (throwing an exception ).

 

2. both Unix and Linux system APIs and Standard C-library functions use the return value method. While some windows APIs use the return value, and some allow callers to pass a flag to determine how to specify an error, for example, heapalloc ().

 

3. java and C # have implemented a complete set of exception handling mechanisms and classes, and most of their class libraries use exception handling methods, therefore, it is not recommended to use the error handling method of the returned value for programming in these two languages, but to encourage the unified use of exceptions;

 

4. to be compatible with C, C ++ does not have a uniform way to handle errors in its library functions, including both returned values and exceptions. It also does not have a set of standard exception classes, it is recommended that developers determine and select the error handling method by themselves.

 

5. The C language itself does not have an easy-to-use exception handling mechanism, and standard library functions use the return value method. If you use Windows API to develop C Programs, you can select windows Seh. However, the problem is that seh is dedicated to Windows and does not work on other operating systems. If you do C Programs in a non-Windows system, some macro-implemented Exception Handling libraries can be used. But after all, C does not set Exception Handling Standards, so it is best to use the traditional return value method to make the program portable and make it understandable to most people.

 

6. whether exceptions are used during development or not, understanding the implementation principles of exception handling is very beneficial to understanding the basic knowledge of function calls and stack manipulation principles. Typical examples are the working principles of setjmp () and longjmp () in the C library.

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.