Waht about subtypes of Exception already defined by the standard API?The only change is in line 11. Try that code and see what happens.Nyeng Gyang wrote:. . the exception . . . is of a programmer-defined checked exception class . . . and a checked exception class defined in the Java core API . . .
So it is always permitted to catch Exception or its supertype Throwable.That JLS section wrote:It is a compile-time error if a catch clause can catch checked exception class E₁ and it is not the case that the try block corresponding to the catch clause can throw a checked exception class that is a subclass or superclass of E₁, unless E₁ is Exception or a superclass of Exception.
Campbell Ritchie wrote:The only change is in line 11. Try that code and see what happens.
Matthew Bendford wrote:I'm not sure about it, but a possible reason could that the compiler optimize it away as the only statement in the try block is declaring a local member, which does not cause any side effect and hence is equal to the whole try/catch be removed. As the try/catch itself is optimized away it doesn't matter to check if the catch is reachable.
What about arithmetic exceptions, when you cannot reason that the divisor isn't 0? What about null pointer exception if you haven't taken precautions to exclude nulls?Stephan van Hulst wrote:. . . The compiler simply does not check . . . unchecked exceptions (even if you, the programmer, can easily reason that it won't). . . .
Campbell Ritchie wrote:What about arithmetic exceptions, when you cannot reason that the divisor isn't 0? What about null pointer exception if you haven't taken precautions to exclude nulls?
Campbell Ritchie wrote:I think it is too difficult a point for “beginning” so I have moved you.
Nyeng Gyang wrote:
Matthew Bendford wrote:I'm not sure about it, but a possible reason could that the compiler optimize it away as the only statement in the try block is declaring a local member, which does not cause any side effect and hence is equal to the whole try/catch be removed. As the try/catch itself is optimized away it doesn't matter to check if the catch is reachable.
The reasoning you have employed here is a quite good, plausible explanation for why the second code chunk does not cause the compile-time error.
Nyeng Gyang wrote:The JLS quote you provided only reinforces the question I asked, namely: Why doesn’t the second code chunk also cause the compile-time error since the code in the try block cannot throw a checked exception of the Exception class or any of its subclasses.
The modification that I have provided to the quote that you have provided is because the exception thrown in the try block may only be either of the same class as, or a subclass of, the exception class provided in the corresponding catch block, but not of a superclass of the exception class provided in the corresponding catch block.
Stephan van Hulst wrote:By their very nature, unchecked exceptions are not checked. The compiler simply does not check whether your try-clause can throw any unchecked exceptions (even if you, the programmer, can easily reason that it won't).
Stephan van Hulst wrote:It is also, sadly, mistaken. Try-statements are never optimized away.
Stephan van Hulst wrote:If you declare FileNotFoundException in a catch-clause, that is absolutely valid if the code in the try-clause can throw IOException, even through IOException is a supertype of FileNotFoundException.
That JLS section wrote:unless E₁ is Exception or a superclass of Exception.
Nyeng Gyang wrote:Have you actually tried this and actually run the code to see that it will compile? I have done so and the code definitely does not compile when the catch block exception is FileNotFoundException and the exception thrown in the try block is IOException.
Probably the same as I posted here.Stephan van Hulst wrote:. . . I don't know what you tried . . .
Obviously the compiler thinks the IOException might include a file not found exception, in which case the catch will be executed. If it isn't banned by the JLS, the compiler has to permit it.Campbell's JShell (JDK15) wrote:Error:
| exception java.io.FileNotFoundException is never thrown in body of corresponding try statement
| catch(FileNotFoundException e)
Stephan van Hulst wrote:
Nyeng Gyang wrote:
Matthew Bendford wrote:I'm not sure about it, but a possible reason could that the compiler optimize it away as the only statement in the try block is declaring a local member, which does not cause any side effect and hence is equal to the whole try/catch be removed. As the try/catch itself is optimized away it doesn't matter to check if the catch is reachable.
The reasoning you have employed here is a quite good, plausible explanation for why the second code chunk does not cause the compile-time error.
It is also, sadly, mistaken. Try-statements are never optimized away.
Stephan van Hulst wrote:The JLS gives you an answer to your question: If you declare Exception (or Throwable) in a catch clause, it is not required that any of the code in your try-clause declares that it throws an exception because Exception is a special case!
Stephan van Hulst wrote:I don't know what you tried, but this compiles just fine:
Stephan van Hulst wrote:I don't know what you tried, but this compiles just fine:
Nyeng Gyang wrote:Just so that we are all clear that you actually understand the gist of the issue I raised in my initial post above, which started this entire discussion thread, kindly use your own words to express/explain this point that I raised. I ask you to do this because virtually all of your responses are not actually addressing the point I raised. Thanks in advance!
Since the method declares (i.e., throws) the exception, my guess is that the compiler would not check to see whether the body of the method actually handles it.
Since the compiler does not perform a check whether the exception thrown in the try block is handled (in the catch block), the compiler does not detect the compile-time error of a programmer throwing an exception of class E in a try block and placing an exception of a subclass of class E in the corresponding catch block
this claim that I made is that the aforementioned JLS quote should be modified with the correction I have provided
It is a compile-time error to throw an exception of class E in a try block and place an exception of a subclass of class E in the corresponding catch block.
When you do this, you will notice that the following is the exact error the compiler reports:
I have checked that JLS link again and it is quoted correctly. It also includes something about superclasses, which Stephan told you about. If you don't understand the JLS, and some of the JLS is hard to understand, please ask for more explanation rather than trying to alter the JLS. All that achieves is to introduce errors into the discussion.Nyeng Gyang wrote:. . . By the way, I believe that there is a small mistake in the explanation provided, in that JLS quote . . . the correct explanation should be as follows, rather than how it is as you have quoted it . . ..
Stephan van Hulst wrote:You are asking why the compiler complains about you catching MyExceptionClass when that exception is not thrown by the code in the corresponding try-clause, while the compiler doesn't complain about you catching Exception, when that exception is not thrown by the code in the corresponding try-clause.
Stephan van Hulst wrote:I threw an exception of class IOException in the try-clause. I caught an exception of a subclass of IOException in the corresponding catch-clause. Where is my compile-time error?
Campbell Ritchie wrote:The method similar to what Stephan wrote seems to compile normally ...
Campbell Ritchie wrote:No, what you have done is move the compiler error. ... but the main() method now has an unhandled exception. So Stephan's argument remains correct.
Campbell Ritchie wrote:Please have another look at the JLS link I posted in my second post yesterday. Does it say anything about exception classes and their supertypes being acceptable as catch parameters?
The earlier examples had a compile time error in the throwIOException() method and you now have an error in the main() method. It is to all intents and purposes the same error.Nyeng Gyang wrote:. . . I am truly not sure what you mean by my having moved the compiler error
The throwIOException() method declares that it might suffer a checked exception and that it cannot handle it itself. The method calling it must therefore deal with that exception if it occurs. So main() must either catch that exception or declare that it might throw it.or by the main() method now having an unhandled exception.
Afraid you are mistaken on both counts. The full range of exceptions possibly thrown is not handled in either method.Anyways, it is not true that the main() method I provided does not handle the exception . . . the IOException, which is thrown in the main() method, is placed in a try block and a corresponding catch block is provided for this try block.
The JLS doesn't claim anything anywhere. It instructs things. If it says that is how catch blocks are to be designed, then all implementers must follow those instructions or their code won't be permissible as a Java® implementation.Nyeng Gyang wrote:. . . The JLS quote definitely claims . . .
Campbell Ritchie wrote:The JLS doesn't claim anything anywhere. It instructs things. If it says that is how catch blocks are to be designed, then all implementers must follow those instructions or their code won't be permissible as a Java® implementation.
Campbell Ritchie wrote:The throwIOException() method declares that it might suffer a checked exception and that it cannot handle it itself. The method calling it must therefore deal with that exception if it occurs.
Nyeng Gyang wrote:(1.) The compiler determines that it is an error to attempt to catch a checked exception, in a catch block, when the code in the corresponding try block cannot possibly throw the checked exception attempted to be caught in the try block.
(2.) The compiler makes the determination mentioned in point number 1 above only when the checked exception in question is of a checked exception class defined in the Java core API, but not when the checked exception is of a programmer-defined checked exception class. (Recall that MyExceptionClass extends Exception.)
(3.) When making the error determination mentioned in point number 1 above, why does the compiler treat a checked exception of a checked exception class defined in the Java core API differently than it treats a checked exception of a programmer-defined checked exception class?
Nyeng Gyang wrote:You only provided a standalone method, not complete code that we can test whether it successfully compiles or not.
Like I have already said, please, attempt to compile the foregoing code and see for yourself that the following is the exact error the compiler reports
Nyeng Gyang wrote:(2.) It is a compile-time error if an exception of class E₁ is thrown in a try block and the corresponding catch block catches a subclass of E₁.
I am afraid you are simply repeating the errors you made earlier.Nyeng Gyang wrote:. . . I will keep repeating myself . . .
Agree. I can see no point in continuing to discuss the topic with you. Sorry.. . . makes our discussion here . . . unproductive. . . . .
Stephan van Hulst wrote:This is true, but the JLS gives a special exception from this rule if the exception to be caught is declared as Exception or Throwable in the catch-clause.
Stephan van Hulst wrote:This is not true. The compiler raises an error for ALL checked exception types, not just programmer defined exceptions, EXCEPT Exception and Throwable.
Java Language Specification wrote:It is a compile-time error if a method or constructor body can throw some exception class E when E is a checked exception class and E is not a subclass of some class declared in the throws clause of the method or constructor.
Stephan van Hulst wrote:
Java Language Specification wrote:It is a compile-time error if a method or constructor body can throw some exception class E when E is a checked exception class and E is not a subclass of some class declared in the throws clause of the method or constructor.
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
|