-6
public static void main(String[] args) {
    try{
        System.out.println("This is try block...!!");
    }
    catch(Exception e)
    {
        System.out.println("Exception is "+e);
    }
    finally
    {
        System.exit(0);//terminates finally block
        System.out.println("This is finally block");
    }
    System.out.println("This is outside the try catch block...");
}

In above code i got output like this "This is try block...!!"

But i want output such that, "This is try block...!!" "This is outside the try catch block...!!"

can any one give me correct solution for this problem? and how can i get output as i want?does any one explain me please?

  • 6
    If you don't want the JVM to exit, don't call System.exit(). It's that simple. – Peter Lawrey Apr 10 '18 at 12:28
  • 3
    you exit the program in `finally` block. `finally` block will always be called, no matter what happens – XtremeBaumer Apr 10 '18 at 12:28
  • 2
    Who told you that "`System.exit(0);` terminates finally block"? – Klitos Kyriacou Apr 10 '18 at 12:28
  • If you don't want the logic of the finally block to execute, why do you have a finally block? – Eran Apr 10 '18 at 12:28
  • *"can any one give me (...) solution for this problem"* What problem? – kryger Apr 10 '18 at 12:29
  • Finally block is always execute no matter if it occur exception or not. So if you executing code System.exit(0); it terminates. So it doesn't go and print the code after finally block. – MinA Apr 10 '18 at 12:37
  • As mentioned, the finally block will always execute right after the end of scope for the `try` or `catch`. `System.exit(0)` will always exit the program, so your code forces an exit of the program before it hits the last line. You do not need the `System.exit(0);` line. But the biggest issue is that you didn't do much to help yourself here. I have some advice in my next comment...... – adprocas Apr 10 '18 at 12:44
  • [Please, do more research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) then **post what you've tried** with a **clear explanation of what isn't** working and provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Read [How to Ask a good question](https://stackoverflow.com/help/how-to-ask). Be sure to [take the tour](https://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/questions/347937/im-new-to-stack-overflow-what-are-some-things-i-should-do-and-what-things-wil). – adprocas Apr 10 '18 at 12:46

4 Answers4

1

it's only for your situation if you want to skip execution of finally block when try is success.

boolean trySuccessflag = false;
         try{
            System.out.println("This is try block...!!");
             trySuccessflag = true;
        }
        catch(Exception e)
        {
            System.out.println("Exception is "+e);
        }
        finally
        {

            if(!trySuccessflag){            
                System.out.println("This is finally block");
            }
        }
        System.out.println("This is outside the try catch block...");
Zealous System
  • 2,080
  • 11
  • 22
0

Your question has already an answer here

Your problem :

First you have to know that finally block is always executed, either you succeed or fail the try block.

Second, System.exit(0) exit the System. So the JVM. Not the Try block. So when you call your program simply terminates that's why nothing more is printed.

Solution :

First if you don't want to execute the Finally block, just do not write it. It makes no sense to write a code you don't want to execute.

Second, if you really need to exit the finally block use break; instead of System.exit(0);

vincrichaud
  • 2,218
  • 17
  • 34
  • i also think like that there is no meaning to put finally block if i want to terminate it but i face this problem in interview question and i gave same answer but she said me for search about it and make me wrong....but however thanks for give me reply – Kinu Virani Apr 11 '18 at 09:11
0

I think it may be so...

public static void main(String[] args) {
try{
    System.out.println("This is try block...!!");
} catch(Exception e) {
    System.out.println("Exception is "+e);
}
System.out.println ("This is outside the try catch block...");

}

Your "System.exit(0);" is before your "System.out.println ("This is outside the try catch block...")". So It doesn't work.

0

Remove this instruction :

System.exit(0);//terminates finally block

It stop the application not terminate the finally block

Jude TCHAYE
  • 434
  • 5
  • 14