views:

207

answers:

7

I have following code

public class TEST
{
  public static void main(String arg[]){
    try
    {
      System.out.println("execute try");
      //what should I write hear that finally does not run.
    }
    catch (Exception e){
      System.out.println(e);
    }
    finally{
      System.out.println("execute finally");
    }
  }
}

what should I write in try or catch block that finally does not run. any idea?

+5  A: 
System.exit(0);
Joachim Sauer
A: 

You need to shutdown the JVM by calling exit as:

System.exit(exit_status);

From the Java docs:

If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

codaddict
How does System.exit() relate to the other answers who are using a boolean flag? Can you provide the of how to use .exit with the asker's code of try-catch-finally?
zengr
+2  A: 

If you want something not to run in the "finally" block - do not put it in "finally". Finally runs always (well, except for a few cases like others have mentioned).

artemb
guessing it's a homework or interview question
invariant
+1 for explaining what the finally block actually does
James
A: 
public class TEST
{
  public static void main(String arg[]){
    bool exitFinally  = false;
    try
    {
      System.out.println("execute try");
      //what should I write hear that finally does not run.
    }
    catch (Exception e){
      System.out.println(e);
    }
    finally{

        if(exitFinally)
            return;

      System.out.println("execute finally");
    }
  }
}
Michael Shimmins
You never set `exitFinally` to true *and* the finally block is still executed, even if it's left before the end.
Joachim Sauer
if you set exitFinally to true then you should write if(!exitFinally);
Sanjeev
The idea was to set exitFinally to true in the appropriate suggestion - I did leave some of it up to the OP.Yes finally is still executed, but immediately returned if required. Other that not implementing finally there is no way around this.
Michael Shimmins
A: 

Put the code in finally into an if.

public class TEST
{
  public static void main(String arg[]){
    boolean b = true;
    try
    {
      System.out.println("execute try");
      if (something()) b = false;
    }
    catch (Exception e){
      System.out.println(e);
    }
    finally{
      if (b){
        System.out.println("execute finally");
      }
    }
  }
}
Mnementh
In these examples, the finally block still runs, you're just conditionally executing some lines within it.
Jack Leow
A: 

Use boolean flag:

public class TEST
{
    public static void main(String arg[]){
        boolean success=false;
        try
        {
            System.out.println("execute try");
            //what should I write hear that finally does not run.
            success=true;
        }
        catch (Exception e){
            System.out.println(e);
        }
        finally{
            if (!success)
            {
                System.out.println("execute finally");
            }
        }
    }
}
Ha
A: 

finally is meant to execute regardless of whether the exception occurs, period. It can't be avoided except by resorting to dubious tactics ( as Joachim said up there ).

If the code you have in the finally block is not meant to be executed every time, don't use a finally construct; Use a simple if-construct instead

Everyone