We know that a try is responsible for outlining code that can cause exceptions, and that the catch is responsible for handling possible exceptions in a try, such as logging an error log, enabling the business to function properly, and finally being responsible for the aftermath of resource release, regardless of whether there are any exceptions that must be executed. It is generally put in the finally. What would happen if catch and finally also get an exception?
Try
{
//java.lang.ArithmeticException
int a = 1/0;
}
catch (Exception e)
{
System.out.println ("catch");
Java.lang.NullPointerException
String value = null;
System.out.println (Value.length ());
}
Finally
{
System.out.println ("finally");
Java.lang.ArrayIndexOutOfBoundsException
int[] array = {1, 2, 3};
System.out.println (Array[6]);
The final result of this code is that it throws a arrayindexoutofboundsexception. The code in try has an exception, and the corresponding catch is executed;
The catch has an exception, it executes finally, and if finally there is an exception, it is thrown directly to the JVM because it is not processed. If this is not used
Finally block, then throws the NullPointerException.
This means that the exceptions that occur in catch and finally will also be thrown directly, and if we do not process them, we will generate an error at runtime. This reminds us that if catch and finally can also have exceptions, then you must use Try-catch again for processing.
The above Java use try-catch-finally some noteworthy things (must SEE) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.