2

Can you tell the idea of how to doing it?

Code :

public void main(String[] args) {
    try {
        //Some Exception throws here
    }
    catch(SomeException se) {
        se.printStackTrace();
    } 
    finally {
        try {
            //SomeException1 throws here
        }
        catch(SomeException1 se1) {
            se.printStackTrace();
            //Control is getting stop in this block itself but i wanna print the below statement
        }

        // I need to print this statement whatever exception occurs
        System.out.println("End Program"); 
    }
}
4
  • 1
    In the second block, are you sure you print "se" and not "se1" ? I dont see any reason for your program to stop after printing se1. Could you give us the actual code ? Commented Feb 11, 2009 at 13:53
  • When you say "//Control is getting stop in this block itself", is it that you kill the execution of the program itself ? (System.exit() or whatever ?) Commented Feb 11, 2009 at 13:57
  • This doesn't make sense, unless you are calling exit() there is no reason for the "End Program" to not be called. Are you sure something other than SomeException1 is not being thrown, which would not be caught and thus not execute the rest of the block. Commented Feb 11, 2009 at 14:37
  • This would mean that control is not stopping where you think it is. This is the only thing that makes sense if the extra finally is making it work. Thus catch (Exception e) in the finally would work too. Commented Feb 11, 2009 at 14:39

5 Answers 5

7

Just add another finally block

public void main(String[] args) {
  try {

    //Some Exception throws here

  }
  catch(SomeException se) {
    se.printStackTrace();
  }
  finally {
    try {

      //SomeException1 throws here

    }
    catch(SomeException1 se1) {
      se.printStackTrace();
    }
    finally {
      System.out.println("End Program"); ----> I need to print this statement whatever exception occurs
    }
  }
}

Or, if you know there are gonna be only the exceptions that you handle, you can just drop the finally blocks altogether.

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

2 Comments

With the code as given, the second finally block should be unnecessary. (Actually, so is the first finally block.)
@mmyers - Yes, it indicates that the question is incorrect and that a different exception is occurring inside the finally that isn't caught.
2

That looks like it should work.

A couple ideas:

  • You can put the System.out.println(...) in another finally block.

  • The only way the program should stop in that catch block is if something calls System.exit(). If you call exit, that's it... no more code runs.

  • The stack traces are written to standard error, but the line you want is written to standard out. Are you redirecting them to different places, maybe?

Comments

2

Adding another finally block should work:

try {
    //Some Exception throws here
} catch(SomeException se) {
    se.printStackTrace();
} finally {
    try {
        //SomeException1 throws here
    } catch(SomeException1 se1) {
        se.printStackTrace();
    } finally {
       System.out.println("End Program"); 
    }
}

Comments

1

How about printing outside of the first try block in your nested try blocks just before main ends?

  public void main(String[] args) {
    try {
      //Some Exception throws here
    } catch(SomeException se) {
      se.printStackTrace();
    } finally {
      try {
        //SomeException1 throws here
      } catch(SomeException1 se1) {
        se.printStackTrace();
        //Control is getting stop in this block itself but i wanna print the below statement
      }
    }

    System.out.println("End Program"); // --- How about here? (Srikanth)  ----
  }

You're catching the exception so your program is not gonna end abruptly anyway. But I guess it will get printed, not sure what you're exactly doing in the nested-catch block. Is it throwing some RuntimeException?

1 Comment

I've updated my answer, see if your code is in the same way. And, do you get some RuntimeException in your nested catch block (the second one)?
0

Some other way:

public void main(String[] args) {
        boolean exceptionOccured = false;
        try {
            exceptionOccured = true;
            //Some Exception throws here
            exceptionOccured = false;
        }
        catch(SomeException se) {
            se.printStackTrace();
        } 
        finally {
            Exception e = null;
            try {
                //SomeException1 throws here
            }
            catch(SomeException1 se1) {                
                exceptionOccured = true;
                se.printStackTrace();
                //Control is getting stop in this block itself but i wanna print the below statement
            }

            // I need to print this statement whatever exception occurs
            if(exceptionOccured)
              System.out.println("End Program"); 
        }
    }

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.