Error handling and exception handling mechanism based on return value

Source: Internet
Author: User
Comparison of error handling and exception handling mechanisms based on return one, error handling mechanism based on return value

For traditional process-oriented languages such as C, the error-handling mechanism based on the return value is usually used, that is, by defining the return value in the program when the program fails, such as 0 for success, and 1 for the operation failure.

The advantage of this error-handling mechanism is that sometimes even when an exception occurs, it may be possible to continue (but the final execution may be incorrect, which makes it difficult to find errors, because sometimes we don't know what is the reason for the program's error, such as the failure of the program to return a value of 0, We see the program output at 0 on the console, but there is no additional information to help us determine the real cause of the program's error;

In addition, when the program is large, it is cumbersome and tedious to check for errors by checking the program return values.

Second, error handling mechanism based on exception handling

For object-oriented languages such as Java, it is common to use exception-catching processing to handle program errors, often by using system-predefined exception classes or by customizing exception classes to throw exceptions when a program fails.

Usually the naming of these exceptions has obvious semantics to indicate the cause of the exception, such as: FileNotFoundException, it is obvious that this exception occurs because the file was not found;

At the same time, for a method that throws a compile-time exception, it forces the method caller to explicitly catch the exception, which enables the program developer to have a clear and clear understanding of the exceptions that may occur during the run of the program, so that When an exception occurs, the program developer can quickly know where the problem is and solve the problem quickly;

In addition, the exception handling mechanism provides a uniform processing mechanism for errors that occur during a program's operation, making it easier to debug programs and more robust programs.

Error handling and exception handling mechanism based on return value

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.