Page 1 of 1

Exception handling

Posted: Sun Nov 22, 2015 11:16 pm
by meghajor
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"?

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");
		   }}
		

Re: Exception handling

Posted: Mon Nov 23, 2015 4:57 am
by admin
Because the exception thrown by m1 is actually caught inside m1 itself and is not propagated outside.