Exception handling
Posted: Sun Nov 22, 2015 11:16 pm
Hi
The output of the below code is : Exception mismatch
m2 finally
Exception in try
In the below code since m1() throws an exception, why does it execute m2()? If the exceptions within m1 and m2 are handled separately due to try block why does it execute the statement "Exception in try"?
The output of the below code is : Exception mismatch
m2 finally
Exception in try
In the below code since m1() throws an exception, why does it execute m2()? If the exceptions within m1 and m2 are handled separately due to try block why does it execute the statement "Exception in try"?
Code: Select all
public class TestClass {
public static void main(String[] args) throws Exception
{
try {
m1();
m2();
}
catch (Exception e)
{ System.out.println("Exception in try");}
}
static void m1() throws Exception {
try {
throw new Exception();
}
catch (Exception et)
{System.out.println("Exception mismatch");}
}
static void m2() throws Exception {
try {
throw new Exception();
}
finally {
System.out.println("m2 finally");
}}