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.

Leo
  • 5,017
  • 6
  • 32
  • 55
  • The runtime exception isn't catched because there is no `try` and line 9 not executed. – Narmer Oct 14 '14 at 07: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");` – Narmer Oct 14 '14 at 07:56

6 Answers6

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).

Community
  • 1
  • 1
Jivi
  • 94
  • 1
  • 3
  • 8
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.

DeepInJava
  • 1,871
  • 3
  • 16
  • 31
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.

Marco
  • 508
  • 2
  • 7
  • 22
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
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

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

Jens
  • 67,715
  • 15
  • 98
  • 113
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.

Manoj Namodurai
  • 529
  • 2
  • 7