Reputation: 10953
I am confused about the behaviour of the CustomExceptions
in below program.If Line2
is commented and Line1
is not then program works well,but if Line1
is commented and Line2
is not then compile time error comes as "Unreachable catch block for CustomChecked.This exception is never thrown from the try statement body" for Line3.Please help me why this comple time exception comes only for the unChecked Exception?.
try
{
if(true)
{
throw new CustomChecked("Checked Exception");// Line1
// throw new CustomUnChecked("Un-Checked Exception");// Line2
}
}
catch(CustomChecked ex) //Line3
{
System.out.println(ex.getMessage());
}
catch(CustomUnChecked ex)
{
System.out.println(ex.getMessage());
}
Exceptions :
class CustomChecked extends Exception
{
public CustomChecked(String msg) {
super(msg);
}
}
class CustomUnChecked extends RuntimeException
{
public CustomUnChecked(String msg) {
super(msg);
}
}
Upvotes: 0
Views: 540
Reputation: 1382
CustomUnChecked is a RuntimeException
which is unchecked
meaning that the complier doesn't check who can/do throw
that exception
. Compiler assumes that every object/method/code block can throw
any type of RuntimeException
and so that you can always check for this type of exceptions in your check
statements.
On the other hand CustomChecked is checked, meaning that the compiler checks for all methods and code blocks that can throw it. So you can only catch
the checked exception if you know (at a complie time) that you invoke a method/code that throws the exception inside the try
block.
So to sum up - your compilator informs you, that the catch
you have in line 3 is not needed (and so should be removed), because there is no way that somebody will throw
CustomCheckedException inside the try statement. As CustomUnCheckedException is not checked
by compiler
(it is runtime, assumed to be thrown unexpected anywere), the check
statement for it can stay.
Upvotes: 1
Reputation: 456
The compiler does not check if you catch RuntimeExceptions, but it does check that you catch Exceptions. Thats why you get an error if you try to catch a non-existant checked exception (if its not in the throws clause, it shouldnt exist, since its a checked exception), and no error if you are catching a RuntimeException (which can happen even through you didnt put it in the throws clause, a NullPointerException for example).
So, in short, CheckedExceptions MUST be declared in the throws clause and MUST be catched. Unchecked exceptions CAN be catched, but the compiler has no way of knowing the if code throws an unchecked exception, so it wont give you an error if you check on it or not.
Upvotes: 0
Reputation: 11925
The part of the Java Spec that is applicable here is 11.2.3. It reads
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.
Notice that this clause is restricted to 'checked' exceptions. Unchecked exceptions, by definition of being 'unchecked' do not have these checks applied to them. Hence the difference in behavior that you are seeing.
Upvotes: 1