Consider the following code
private int meth()
{
try
{
return 1;
}
catch(Exception ex)
{
return 2;
}
finally
{
return 3;
}
}
When the aforeseen code is compiled, "Exception" is treated as unchecked exception. That is "unreachable catch block Exception is never thrown in try block" compilation error does not occur.Consider I am declaring my own exception,
class MyException extends Exception
{
}
and using it in the code
private int meth()
{
try
{
return 1;
}
catch(MyException me)
{
return 2;
}
finally
{
return 3;
}
}
In this "unreachable catch block MyException is never thrown in try block" compilation error occurs. Why in the first scenario "Exception" is treated as RuntimeException and in the second scenario even though "MyException" is a subclass of "Exception" it is being treated as checked exception. Can someone please help me to resolve this issue?