0

I believe that java.lang.Exception is also a checked exception.But there is difference in behaviour between java.lang.Exception vs any other checked exception such as IOException or SQLException.

See the following code compiled with Java version 7

 try {
    //empty try block   
    } catch (SQLException e) {

        e.printStackTrace();
    }

This gives the following comilation error:-

java.lang.Error: Unresolved compilation problem: Unreachable catch block for SQLException. This exception is never thrown from the try statement body

but the same code doesnt give any compilation error if there is no statement in try block:-

       try {
        // empty try block
    } catch (Exception e) {

        e.printStackTrace();
    }

*Result:- No compialtion error *

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • Please check this http://stackoverflow.com/questions/2190161/difference-between-java-lang-runtimeexception-and-java-lang-exception – Arunkumar Nov 29 '13 at 13:01

2 Answers2

2

The distinction between unchecked and checked exceptions is being made by "checking" whether the exception is a subtype of RuntimeException or not.

Additionally, RuntimeException itself is a subtype of Exception. So Exception covers all checked and all unchecked exceptions. So, in your code example, the compiler is not allowed to complain.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0

SqlException is specific exception as compare to Exception which is generic. In case when you are trying to catch more specific exception, compiler would be checking the code in try to see if code can lead to that specific exception. But in case of Exception, it is generic and all the exception are child to Exception hence compiler ignores it as it is not specific to any particular piece of code....

I hope this helps..

Prashant Thakkar
  • 1,383
  • 1
  • 12
  • 15