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