Exception Tricky questions


I am going to deal with few tricky questions regarding an exceptions.


What is the output of the below program

  1. package com.kb.exception;
  2.  
  3. public class Exception1 {
  4.  
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         try {
  10.             int i=9/0;
  11.             System.out.println(i);
  12.             return;
  13.         } catch (Exception e) {
  14.             System.out.println("exception caught");
  15.             return;
  16.         }
  17.         System.out.println("task completed");
  18.  
  19.     }
  20.  
  21. }
package com.kb.exception;

public class Exception1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			int i=9/0;
			System.out.println(i);
			return;
		} catch (Exception e) {
			System.out.println("exception caught");
			return;
		}
		System.out.println("task completed");

	}

}


if your ans is as below

exception caught or task completed

then you are wrong, because there is a return in try block so it makes the last line as unreachable code. So compile time error.

If we remove return from try then above output will come.

Ground rule -> if there is a return statement in try then no code after try- catch will be valid.

Can you try the below program

  1. package com.kb.exception;
  2.  
  3. public class Exception2 {
  4.  
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         String returnVal = method1();
  10.         System.out.println(returnVal);
  11.     }
  12.  
  13.     public static String method1() {
  14.         try {
  15.             int i = 9;
  16.             System.out.println(i);
  17.             return "from try";
  18.         } catch (Exception e) {
  19.             System.out.println("exception caught");
  20.             return "from catch";
  21.         } finally {
  22.             System.out.println("finally block executing");
  23.         }
  24.     }
  25.  
  26. }
package com.kb.exception;

public class Exception2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String returnVal = method1();
		System.out.println(returnVal);
	}

	public static String method1() {
		try {
			int i = 9;
			System.out.println(i);
			return "from try";
		} catch (Exception e) {
			System.out.println("exception caught");
			return "from catch";
		} finally {
			System.out.println("finally block executing");
		}
	}

}


here try block started it’s execution and no exception so it goes to finally and then return back to caller.

Now try below program and see the defference

  1. package com.kb.exception;
  2.  
  3. public class Exception2 {
  4.  
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         String returnVal = method1();
  10.         System.out.println(returnVal);
  11.     }
  12.  
  13.     public static String method1() {
  14.         try {
  15.             int i = 9;
  16.             System.out.println(i);
  17.             return "from try";
  18.         } catch (Exception e) {
  19.             System.out.println("exception caught");
  20.             return "from catch";
  21.         } finally {
  22.             System.out.println("finally block executing");
  23.             return "from finally";
  24.         }
  25.     }
  26.  
  27. }
package com.kb.exception;

public class Exception2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String returnVal = method1();
		System.out.println(returnVal);
	}

	public static String method1() {
		try {
			int i = 9;
			System.out.println(i);
			return "from try";
		} catch (Exception e) {
			System.out.println("exception caught");
			return "from catch";
		} finally {
			System.out.println("finally block executing");
			return "from finally";
		}
	}

}



Now see the difference , output’s last line got changed. Why ?

Even though try has return statement finally block gets a chance to execute before try’s return statement and finally has return statement so it returns “from finally” to caller.

Ground rule

1) If exception does not occur then try continues the execution til the last line before the return statement in try block and then 

control goes to finally block, if finally does not have any return then control comes back to try’s return statement and hence completes. 

If finally has return statement then flow returns from finally it never reaches back to try’s return statement.

 2) If exception occurs in try block then control jumps to catch block and if catch has return statement then flow excecutes until last line 

before return in catch , then control goes to finally, if finally has return then control returns to caller and if finally does not have return

then control goes back to return statement of catch block.
Based on above explanation find the output of the below program

  1. package com.kb.exception;
  2.  
  3. public class Exception2 {
  4.  
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         String returnVal = method1();
  10.         System.out.println(returnVal);
  11.     }
  12.  
  13.     public static String method1() {
  14.         try {
  15.             int i = 9/0;
  16.             System.out.println(i);
  17.         } catch (Exception e) {
  18.             System.out.println("exception caught");
  19.             return "from catch";
  20.         } finally {
  21.             System.out.println("finally block executing");
  22.         }
  23.         System.out.println("end");
  24.         return "from end";
  25.     }
  26.  
  27. }
package com.kb.exception;

public class Exception2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String returnVal = method1();
		System.out.println(returnVal);
	}

	public static String method1() {
		try {
			int i = 9/0;
			System.out.println(i);
		} catch (Exception e) {
			System.out.println("exception caught");
			return "from catch";
		} finally {
			System.out.println("finally block executing");
		}
		System.out.println("end");
		return "from end";
	}

}


guess..??

guess…????

good if you say output as below

Ground rule
If there is a return statement in try/catch/finally then it will stop the further execution of the program.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on