Doubt in exception handling and finally block in java

旧时模样 提交于 2019-12-11 04:09:45

问题


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:


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.




回答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?




回答3:


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



回答4:


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?




回答5:


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


来源:https://stackoverflow.com/questions/536811/doubt-in-exception-handling-and-finally-block-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!