Exception handling

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
meghajor
Posts: 7
Joined: Fri Nov 20, 2015 7:03 pm
Contact:

Exception handling

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

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Exception handling

Post by admin »

Because the exception thrown by m1 is actually caught inside m1 itself and is not propagated outside.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests