4

It might be easy but I don't understand why the output is coming as 1 4. And what is the function of the return statement at Line 9?

public static void main(String[] args) {
    try{
        f();
    } catch(InterruptedException e){
        System.out.println("1");
        throw new RuntimeException();
    } catch(RuntimeException e){
        System.out.println("2");
        return;                    \\ Line 9
    } catch(Exception e){
        System.out.println("3");
    } finally{
        System.out.println("4");
    } 
        System.out.println("5");
}   
        static void f() throws InterruptedException{
            throw new InterruptedException("Interrupted");
    }

Thanks in advance.

2
  • The runtime exception isn't catched because there is no try and line 9 not executed. Commented Oct 14, 2014 at 7:50
  • Also 5 is not going to be printed because RuntimeException is an unchecked exception, thus is internally catched (without explicit try-catch block) and terminating the program before System.out.println("5"); Commented Oct 14, 2014 at 7:56

6 Answers 6

2

Your function f() throws InterruptedException, which is caught by the first catch block (hence it prints 1), but this catch block cannot throw other exceptions (if it is not thrown by your method), Hence, no other catch block can catch your excception and therefore finally is executed (finally executes in every case except those silly infinite loop cases). you can refer to Exception thrown inside catch block - will it be caught again?.

I hope it helps.

Just to summarize, you can throw any exception from try block & it will be caught (if there is a good catch block). But from catch block only those exceptions can be thrown (and consequently caught by) which your method throws.

If you throw exception from catch block which are not thrown by your method, it is meaning less and won't be caught (like in your case).

Sign up to request clarification or add additional context in comments.

Comments

1

As you can see f() throws InterruptedException, so it will first print 1 which is inside first catch block and then finally would execute so it will print 4.

Comments

1

The 1 is printed because f() throws an InterruptedException. Because the Exception is handled in the first catch block it is not handled in the other exception blocks belower anymore. The finally statement is always run, so the 4 is printed too.

Comments

1
try{
    f();
} catch(InterruptedException e){
    System.out.println("1");
    throw new RuntimeException(); // this RuntimeException will not catch by 
                                  //following catch block
} catch(RuntimeException e){

You needs to change your code as follows to catch it.

 try{
        f();
    } catch(InterruptedException e){
        System.out.println("1");
        try {
            throw new RuntimeException();// this RuntimeException will catch 
                                         // by the following catch 
        }catch (RuntimeException e1){
            System.out.println("hello");
        }
    } catch(RuntimeException e){
        System.out.println("2");
        return;
    } catch(Exception e){
        System.out.println("3");
    } finally{
        System.out.println("4");
    }
    System.out.println("5");

Then your out put"

  1
  hello // caught the RunTimeException
  4 // will give you 2,but also going to finally block then top element of stack is 4
  5 // last print element out side try-catch-finally

Comments

1

f() throws an InterruptedException so you get the 1. finally is always called at the end so you get the 4.

Comments

1

As f() throw InterruptedException which executes,

catch(InterruptedException e){
        System.out.println("1");
        throw new RuntimeException();
}

prints --> 1

and finally gets executed before exiting the program,

finally{
        System.out.println("4");
}

prints --> 4

Code after return wont execute but finally will get executed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.