In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Java exception handling how to write a "correct" but by the compiler that there are syntax errors in the program, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
The title of the article seems to be contradictory, but I put quotation marks on the word "correct". Let's look at an example of some knowledge about Java exception handling (Exception Handling).
Take a look at the following program. The method pleaseThrow accepts an instance of Exception and then simply throws it. Then when I call this method, I pass in an instance of SQLException. Because the call to pleaseThrow is wrapped in a try catch block
Question: can the SQLException thrown by the plesseThrow method be successfully occupied by catch?
Public class ExceptionForQuiz {private void pleaseThrow (final Exception t) throws T {throw (T);} public static void main (final String [] args) {try {new ExceptionForQuiz () .pleaseThrow (new SQLException ());} catch (final SQLException ex) {System.out.println ("Jerry print"); ex.printStackTrace ();}
Answer: the above code has syntax errors and cannot be compiled!
Let's analyze it step by step.
The Java class ExceptionForQuiz uses a generic syntax, which means that when the generic class is instantiated, the type parameter T passed in must be Exception and its subclasses.
When I instantiated the class ExceptionForQuiz, the type parameter passed in was RuntimeException.
RuntimeException is a Unchecked exception in Java, and even though a method may throw a RuntimeException when it is running, it does not require the developer to explicitly declare it in front of the method.
Look, JDK RuntimeException's notes make it 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 big cow.
Because generics are a concept introduced in Java 1.5, there is a concept of type erasure about generics, that is, generics information exists only in the code compilation phase, and the information related to generics is erased in the compiled code. For example, if the type parameter part of the previous generic class does not specify an upper limit, it will be translated into a normal Object type. If an upper limit is specified, the type parameter is replaced with the type upper limit.
For the sake of simplicity, let's remove the try catch block from the code.
Here is the code decompiled from ExceptionForQuiz.class:
We can see from the figure above that the generic parameter RuntimeException of methods pleaseThrow and Ray ExceptionForQuiz has been erased. The type of exception that can be thrown by the pleaseThrow method has been erased to Exception.
Using javap to observe the bytecode generated by compilation, 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.
Based on the fact that the exception type is erased, this error message is reasonable, because the declaration of the pleaseThrow method can now only throw an exception of type Exception, so the catch on line 14 will never be able to receive an exception of type SQLException, so the compiler throws an error.
How to eliminate this compiler error? Just change the SQLException in line 14 to RuntimeException.
But in this case, although the syntax error is eliminated, the SQLException thrown by the method pleaseThrow cannot be held by catch and will report a runtime error:
How to catch the SQLException thrown by pleaseThrow with catch statement? Change the RuntimeException in line 14 to the superclass of all exceptions: Exception.
Execute again, this time with neither syntax nor runtime errors: SQLException has been successfully caught by the catch statement on line 14.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.