This article brings you a detailed introduction to Java exception handling. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Let’s take a look at an example and some knowledge points about Java exception handling (Exception Handling).
Look at the program below. The method pleaseThrow accepts an instance of Exception and simply throws the instance. Then when calling this method, I passed in an instance of SQLException. Because the call to pleaseThrow is wrapped in a try catch block,
Question: Can the SQLException thrown by the pleaseThrow method be successfully caught?
public class ExceptionForQuiz<t> { private void pleaseThrow(final Exception t) throws T { throw (T) t; } public static void main(final String[] args) { try { new ExceptionForQuiz<runtimeexception>().pleaseThrow(new SQLException()); } catch( final SQLException ex){ System.out.println("Jerry print"); ex.printStackTrace(); } } }</runtimeexception></t>
Answer: The above code has a grammatical error and cannot be compiled!
Let’s analyze it step by step.
The Java class ExceptionForQuiz
When I instantiate the class ExceptionForQuiz, the type parameter passed in is RuntimeException.
RuntimeException is an Unchecked exception in Java. Even if a RuntimeException may be thrown when a method is running, developers do not need to explicitly declare it in code before the method.
Look at the comments of JDK RuntimeException and it is very clear: Unchecked exceptions do NOT need to be declared in a method or constructor's clause if they can be thrown by the execution of the method or constructor.
The author Frank Yellin must be a great guy.
Because generics are a concept introduced in Java 1.5, there is a concept of type erasure for generics, that is, generics The information only exists in the code compilation phase. In the compiled code, the information related to generics will be erased. For example, if the type parameter part in the previous generic class does not specify an upper limit, written like this
For the sake of simplicity, we first remove the try catch block in the code.
The following is the code after decompilation from ExceptionForQuiz.class:
We can observe from the above figure that the generic parameter RuntimeException of methods pleaseThrow and RayExceptionForQuiz has been erased. The exception type that can be thrown by the pleaseThrow method has been erased and becomes Exception.
Use javap to observe the bytecode generated by compilation, and you can also find the fact that the type parameter RuntimeException has been erased:
Look at the second red highlighted area: Exceptions: throw java.lang. Exception
Now let’s see what error message the compiler will report: Unreachable catch block for SQLException. This exception is never thrown from the try statement body .
This error message is reasonable based on the fact that exception type is erased, since the declaration of the pleaseThrow method can now only throw exceptions of type exception, so the catch on line 14 can never receive an exception of type SQLException, so the compiler throws an error.
How to eliminate this compiler error? Just change the SQLException on line 14 to RuntimeException.
But in this case, although the syntax error is eliminated, the SQLException thrown by the pleaseThrow method cannot be caught, and a runtime error will be reported:
How to use the catch statement to catch the SQLException thrown by pleaseThrow? Change RuntimeException on line 14 to the superclass of all exceptions: Exception.
Execute again, this time there is neither syntax error nor runtime error: SQLException has been successfully caught by the catch statement on line 14.
The above is the detailed content of Detailed introduction to Java exception handling. For more information, please follow other related articles on the PHP Chinese website!