Early programming languages, such as C, do not have exception handling, usually by encountering errors that return a particular value or setting a flag, and to determine whether there is an error. As the scale of the system expands, this error handling has become an obstacle to creating large maintainable programs. So in some languages, exception handling mechanisms, such as the exception handling statement in basic "On Error Goto", and Java in C + + based on the establishment of a new exception handling mechanism.
Java makes exception handling through object-oriented method, classifies different kinds of anomalies, and provides good interface. This mechanism provides a powerful control method for complex programs. At the same time, these exception codes are separated from "normal" code, which enhances the readability of the program and makes it more flexible to write the program.
Exception handling also has the advantage that when you are unsure and handling an exception, you can not deal with it and submit the problem. On the other hand, the exception handling mechanism makes the error-handling code more organized and easier to maintain. Next, let's look at how exceptions are handled in Java.
Exception classes in Java
In Java, each exception is an object, which is an instance of a Throwable class or other subclass. When a method has an exception, an exception object is thrown, which contains exception information, and the method that invokes the object can catch the exception and process it.
The Throwable class has two standard subclasses: Java.lang.Error and Java.lang.exception, that is, errors and exceptions. Error error classes generally refer to virtual machine-related issues such as system crashes, virtual machine errors, dynamic link failures, etc., which cannot be recovered or cannot be captured, resulting in application disruption. The exception exception class refers to some exceptions that can be caught and may be recovered, such as the subscript arrayindexoutofboundsexcepton, the number 0 in addition to the abnormal arithmeticexception, input/ Output abnormal ioexception and so on.
Tip: The Java compiler requires that Java programs must capture or declare all runtime exceptions, such as FileNotFoundException, IOException, and so on. Because, for this kind of exception, if the program does not process, may bring unexpected results. However, runtime exceptions can be unhandled because such exceptions are common, and all processing can have an impact on the readability and operational efficiency of the program.
Java Exception Handling form
Java exception handling is done with 5 keywords: try, catch, throw, throws, and finally. Typically, a try is used to execute a program, and if an exception occurs, the system throws (throws) an exception, at which point you can catch (catch) it by its type, or the last (finally) by the default handler.
The following are the basic forms of the Java exception handler:
try
//执行时程序块
catch ExceptionType1e
//对ExceptionType1的处理
catch ExceptionType2e
//对ExceptionType2的处理
throw e
//再抛出这个“异常”
finally
Try program blocks and catch statements
When a try statement is encountered, the "exception" frame is placed on the stack until all the statements within the try block are complete. If a try statement at the next level does not handle an "exception", the stack expands until it encounters a try statement that handles this "exception". After the try program, you should include a catch clause to specify the type of "exception" you want to catch.
Tip: The first step in catching an exception is to use Try ... Select the range where you want to catch the exception, and when executed, the code in parentheses produces an exception object and is thrown. Then you can use a catch block to handle the exception.
Throw statements and throws statements
The throw statement is used to explicitly throw an "exception". First, you have to get an instance handle generated by a Throwable class or other subclass, pass parameters to a catch clause, or create an instance with the new statement.