4

There are three cases to be considered :

Case 1:

public class Test {

    public static void main(String[] args) {
                System.out.println(1);
                System.out.println(2);
                return;
                System.out.println(3);
    }
}

Case 2:

public class Test {

    public static void main(String[] args) {

        try{
            System.out.println(1);
            return;
        }finally{
            System.out.println(2);
        }
        System.out.println(3);
    }
}

Case 3:

public class Test {

    public static void main(String[] args) {
        try{
            System.out.println(1);
            return;
        }catch(Exception e){
            System.out.println(2);
        }
        System.out.println(3);
    }
}

I understand that in case 1 statement System.out.println(3) is unreachable that's why it is a compiler error, but why compiler does not shows any error in case 3.

Also note that it is even a compiler error in case 2.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
webspider
  • 67
  • 1
  • 6

4 Answers4

5

Case 3:

If exception is raised than all your code is available and it prints 1,2,3. That's the reason why you don't have any error (unreachable code) there.

Case 2:

In this case no matter what you won't reach System.out.println(3), because you always return from main method.

Jakub H
  • 2,130
  • 9
  • 16
4

In case 2 you have a finally clause. It is executed after the try clause. So the execution order is:

  1. System.out.println(1);
  2. return;
  3. System.out.println(2);

And the "System.out.println(3);" is unreachable.

But in case 3 you have a cath clause. It is executed if there is a Exeption in the try clause. So there are to possible ways to go (with or without error on "System.out.println(1);")

First without error:

  1. System.out.println(1);
  2. return;

Second with error:

  1. System.out.println(1); (Throws a Execption)
  2. System.out.println(2); (Does not exit the code by return or throwing a new exeption)
  3. System.out.println(3); (after the try/catch)

PS.: In case 2 if you had a Exception on System.out.println(1); he would run the System.out.println(2); just before continue to throw the exception up to the stack trace...

LgFranco
  • 1,004
  • 6
  • 14
1

In case 3, if println(1) throws RuntimeException, println(2) is executed, and then println(3) is executed. So, println(3) is reachable.

fajarkoe
  • 1,543
  • 10
  • 12
1

in your case 1 you have unreachable code because no matter what happens you are going to return from the method but in case 3 you may execute the statement System.out.println(3) if exception occures

case 3:

if you are going to get exception in System.out.println(1) then eventually control will be shifted to catch block and will execute System.out.println(2) and then with normal flow it will execute System.out.println(3)

now concider second case:

if you do not get any exception in try block it will execute System.out.println(1) and then System.out.println(3)

i think there is no need of return inside try block because you have to write code after println(1) which should execute when there is no exception in println(1)

so good practice is to write code as follow

public class Test {

    public static void main(String[] args) {
        try{
            System.out.println(1);
            System.out.println(3);
        }catch(Exception e){
            System.out.println(2);
        }
    }
}

so what exactly i mean is the statements which are dependent on execution of System.out.println(1) should also be inside try after System.out.println(1) and the statements which are dependent on the code inside try should be written outside try-catch

Amar Magar
  • 840
  • 1
  • 11
  • 15