Exception 2 checked for behavior

I noticed that there are two kinds of checked exceptions:

one)

public static void main (String []args) { try{ //no exception thrown here, it still compiles though. } catch(Exception e){System.out.println(e);} } 

2)

 public static void main (String []args) { try{ // it does not compile if not written any code which potentially might throw it } catch(IOException io){System.out.println(io);} } 

Is there any rule to predict this behavior? know in advance what is optional for attendance, and is that so? I hope I have clearly explained this problem. Thanks, Indeed, ItIs

+4
source share
3 answers

Java Language Specification Approves

This is a compile-time error if the catch clause can be checked for the exception class E1, and this is not the case when the try block corresponding to the catch clause can call the checked exception class, that is, the subclass or superclass E1 , if E1 is no exception or the superclass Exception.

+4
source

The code compiles fine with Exception because it is the superclass of all Checked and Unchecked exceptions. Thus, a catch clause with Exception can also handle an unchecked exception. And for an Unchecked exception, the compiler cannot check at compile time that the exception is thrown or not. Thus, this will not give any error for Exception .

You can even try this with a RuntimeException :

 try { } catch (RuntimeException e) { } 

... this will compile too.

From JLS Section 11.2.3 - Exception Check :

This is a compile-time error if the catch clause can catch the checked exception class E1, and this is not the case when the try block corresponding to the catch clause can throw the checked exception class, which is a subclass or superclass of E1, if E1 is the exception or superclass of the exception.

+3
source

Exception is only a checked exception, but it is also a superclass for RuntimeException and other unchecked exceptions.

1) In the first case, since the hierarchy of Exception classes includes a RuntimeException , the compiler cannot conclude that Exception will not be thrown in your try { ... } block try { ... } . Therefore, it must allow the code.

It can confirm a positive case (if you call a method that declares a throws Exception or a nonclassing subclass), but it cannot confirm a negative case.

2) In the second case, at compile time, we can conclude that IOException will not be thrown in the try { ... } block, because IOException (or any other exception except Exception ) is not a superclass for any runtime exceptions.


From the following behavior, I believe that Exception is the only special case with a combination of two types:

If thrown:
1A. No need to declare or 1B. To be announced

If announced:
2A. No need to drop or 2B. Must be cast

If not selected:
3A. May be caught or 3B. Can't be caught

  • RuntimeException: 1A , 2A , 3A
  • Any other exception excluded: 1B , 2B , 3B
  • Exception: 1B , 2A , 3A

Additional information on checked and unchecked exceptions in this answer: fooobar.com/questions/14027 / ...

+2
source

All Articles