Sanjeev
Sanjeev

Reputation: 1097

Java: I have try,catch and finally in a java code and I want after try or catch block finally does not execute

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?

Upvotes: 1

Views: 1362

Answers (7)

Everyone
Everyone

Reputation: 2376

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

Upvotes: 0

Ha.
Ha.

Reputation: 3454

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");
            }
        }
    }
}

Upvotes: -1

Mnementh
Mnementh

Reputation: 51311

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");
      }
    }
  }
}

Upvotes: -1

Michael
Michael

Reputation: 20049

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");
    }
  }
}

Upvotes: -1

artemb
artemb

Reputation: 9371

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

Upvotes: 2

codaddict
codaddict

Reputation: 455020

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.

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 308031

System.exit(0);

Upvotes: 5

Related Questions