views:

569

answers:

6

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"); 
    }
}
+1  A: 

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?

artknish
Yeah.. I have tried that also but its not printing!!!!!
raja
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)?
artknish
+2  A: 

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?

TREE
+6  A: 

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.

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

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"); 
    }
}
Ryan Emerle
+1  A: 

Yeah.. I have put another finally block n its working fine. Thanks all.

raja
Can you accept jpalecek's answer?
Ryan Emerle
A: 

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