Using e.printStackTrace() in Java

前端 未结 5 520
感情败类
感情败类 2021-02-01 12:31

This is probably a newbie question, but hope you can help me. :) I have something like this:

try
{ 
//try to do something there
}
catch (IOException e)
{
//handl         


        
5条回答
  •  一生所求
    2021-02-01 12:50

    Just printing a stack trace is not enough. Printing the exception's stack trace in itself doesn't mean that it is completely bad practice, but printing only the stack trace when an exception occurs is an issue.

    Always log exceptions(using a good logging framework), but do not expose them to the end-user. And keep ensure that showing stack traces only in development mode.

    I myself use(most of the time) logger.log(Level.SEVERE, .getMessage(), );.

    when netbeans suggest you to handle the exception 'Surround Statement with try-catch', if you click on this, it will generate):

    try {
        //something need to be handle(exception that throw)
    } catch (SQLException ex) {
        Logger.getLogger(ClassName.class.getName()).log(Level.SEVERE, null, ex);
    }
    

    Which is better than ex.printStackTrace();.

    These may help:

    • Best Practices for Exception Handling
    • Javarevisited- logging's.
    • What are the latest options in Java logging frameworks?
    • Benchmarking Java logging frameworks.

提交回复
热议问题