Exception
and IOException
both are compile time checked exceptions.
But, we cant use IOException
within catch block. But we can use Exception
within catch block what is the reason for it.
import java.io.*;
class Demo{
public static void main(String args[]){
try{
}catch(IOException e){ // Does not compile
}
try{
}catch(Exception e){ // Compile
}
}
}
You cannot catch a checked exception that is never thrown in a try block, except for Exception
(or Throwable
). This behavior is specified by the JLS, Section 11.2.3:
It is a compile-time error if a catch clause can catch checked exception class E1 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 E1, unless E1 is Exception or a superclass of Exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With