Home >Java >Javagetting Started >Detailed introduction to exception types and exception handling in java
1. Exception implementation and classification
##1. First look at the structure diagram of the exception class
The above picture can simply show the exception class implementation structure diagram. Of course, the above picture does not include all exceptions. Users can also customize their own exception implementation. The above picture is enough to help us explain and understand the implementation of exceptions: java related free video tutorials: 1. All exceptions are inherited from Throwable It is the common ancestor of all anomalies. 2.Throwable has two subclasses,
Error and
Exception.
Virtual MachineError), class definition error (
NoClassDefFoundError), etc. .
Exception
Checked Exception
IOException, or some custom exceptions. Except for
RuntimeException and its subclasses, they are all checked exceptions.
Unchecked Exception
RuntimeException and its subclasses are all unchecked exceptions. For example, NPE null pointer exception, arithmetic exception when the divisor is 0
ArithmeticException, etc. This kind of exception occurs during runtime and cannot be caught and handled in advance. Error is also an unchecked exception and cannot be handled in advance.
2. Exception handling
Exception handling in the code is actually the handling of checkable exceptions. 1. Processed through try...catch statement block:try { // 程序代码 }catch(ExceptionName e1) { //Catch 块 }The Catch statement contains a statement of the exception type to be caught. When an exception occurs in the protected code block, the catch block following the try is checked. If the exception that occurs is contained in a catch block, the exception will be passed to the catch block, which is the same as passing a parameter to a method. 2. In addition, you can also throw it directly without processing it at the specific location, and then handle it through throws/throw to the upper layer. Specifically, if a method does not catch a checked exception, then the method must Use the throws keyword to declare. throws keyword is placed at the end of the method signature. You can also use the throw keyword to throw an exception, whether it is newly instantiated or just caught. The declaration of the following method throws a RemoteException exception:
import java.io.*; public class className { public void deposit(double amount) throws RemoteException { // Method implementation throw new RemoteException(); } //Remainder of class definition }3. finally keywordThe finally keyword is used to create a code block that is executed after the try code block. Regardless of whether an exception occurs, the code in the finally block will always be executed. In the finally code block, you can run cleanup statements such as cleanup types. The finally code block appears at the end of the catch code block, and the syntax is as follows:
try{ // 程序代码 }catch(异常类型1 异常的变量名1){ // 程序代码 }catch(异常类型2 异常的变量名2){ // 程序代码 }finally{ // 程序代码 }Recommended java related article tutorials:
The above is the detailed content of Detailed introduction to exception types and exception handling in java. For more information, please follow other related articles on the PHP Chinese website!