AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-exception-handling: How compiler identifies that there is a checked exception?

Please explain in detail How compiler identifies that there is a checked exception.

java x 211
exception-handling x 24
Posted On : 2016-04-20 20:29:30.0
profile Mantu Kumar - anyforum.in Mantu Kumar
8200
up-rate
5
down-rate

Answers


The point behind how compiler shows error for checked exception at compile time is it has only one rule- "if calling method/constructor throws an exception it must be handled". For Exception checking compiler performs below checks:

1. 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.

2. It is a compile-time error if a class variable initializer or static initializer of a named class or interface can throw a checked exception class.

3. It is a compile-time error if an instance variable initializer or instance initializer of a named class can throw a checked exception class unless that exception class or one of its superclasses is explicitly declared in the throws clause of each constructor of its class and the class has at least one explicitly declared constructor.

Note:
----------------------------------
that no compile-time error is due if an instance variable initializer or instance initializer of an anonymous class can throw an exception class. In a named class, it is the responsibility of the programmer to propagate information about which exception classes can be thrown by initializers, by declaring a suitable throws clause on any explicit constructor declaration. This relationship between the checked exception classes thrown by a class´s initializers and the checked exception classes declared by a class´s constructors is assured implicitly for an anonymous class declaration, because no explicit constructor declarations are possible and a Java compiler always generates a constructor with a suitable throws clause for that anonymous class declaration based on the checked exception classes that its initializers can throw.

4. 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.

5. It is a compile-time error if a catch clause can catch checked exception class E1 and a preceding catch clause of the immediately enclosing try statement can catch E1 or a superclass of E1.

Note:
------------------------
A Java compiler is encouraged to issue a warning if a catch clause can catch checked exception class E1 and the try block corresponding to the catch clause can throw checked exception class E2, a subclass of E1, and a preceding catch clause of the immediately enclosing try statement can catch checked exception class E3 where E2 <: E3 <: E1.

-Oracle Docs

For example let´s understand the point how compiler shows error/warning for checked exception at compile time. Whenever compiler shows error for any checked exception ultimately internally that method or constructor throws exception and that must be handled.

Like if we see getDrivers() method of DriverManager class, it does not throw any exception and when we use this method compiler´ll not show any error. and if we use getConnection(String url) of DriverManager class it throws SQLException, and if we don´t handle it, compiler´ll show error. So it´s not something like whenever we use DriverManager class we need to put it in try block. It´s all about the method/constructor you are going to call throws exception or not.

Posted On : 2016-04-20 23:00:50
Satisfied : 1 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188242009
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in