Exception tricky questions continued

Tricky question 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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");
        }
    }
 
}
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");
		}
	}

}

Output is

here try block started its execution and no exeption so it goes to finally and then return back to caller.

Now tricky question 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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";
        }
    }
 
}
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 : try execution starts

2 cases

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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/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";
    }
 
}
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

Karibasappa G C (KB)
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