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:
java teaching video
1. All exceptions are inherited from Throwable It is the common ancestor of all anomalies.
2.
Throwable has two subclasses,
Error and
Exception.
Where Error is an error, all compile-time errors and system errors are thrown through Error. These errors indicate that the fault occurs in the virtual machine itself, or occurs when the virtual machine attempts to execute an application, such as Java virtual machine running error (
Virtual MachineError), class definition error (
NoClassDefFoundError), etc. .
These errors are uncheckable because they are outside the control and processing capabilities of the application, and most of them are situations that are not allowed to occur when the program is running. For a well-designed application, even if an error does occur, there should be no attempt to handle the exception condition it caused. In Java, errors are described through subclasses of Error.
3.
Exception
is another very important exception subclass. The exceptions it specifies are exceptions that the program itself can handle.
The difference between exceptions and errors is that exceptions can be handled, but errors cannot be handled.
4.
Checked Exception
Checkable exceptions are very commonly used when coding. All checked exceptions need to be handled in the code. Their occurrence is predictable. It is a normal situation and can be handled reasonably, such as
IOException, or some custom exceptions. Except for
RuntimeException and its subclasses, they are all checked exceptions.
5.
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 块
}
Copy after login
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
}
Copy after login
3. finally keyword
The 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{
// 程序代码
}
Copy after login
Recommended java related article tutorials:
java entry program
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!