简体   繁体   English

什么要包含在Exception的catch子句中

[英]What to include in the catch clause of Exception

I have a code that throws a bunch of Exceptions but each of them only contains a printStackTrace() method as shown below 我有一个代码抛出一堆异常,但每个异常只包含一个printStackTrace()方法,如下所示

} catch (SecurityException e) {
    // TODO Auto-generated catch block
    System.err.println(e);
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Is this sufficient or do I need to include additional statements like System.err.println(e)? 这是否足够,还是需要包含System.err.println(e)等其他语句? Usually, if an exception occurs I am able to trace the source with the above alone. 通常,如果发生异常,我可以单独使用上述内容跟踪源。

If there is something you can do to solve the problem, do it in the catch, if there is nothing you can do, then it is better to use a logging framework to register the exception than to use e.printStackTrace(); 如果有什么办法可以解决问题,那么在catch中执行它,如果没有什么可以做的话,那么最好使用日志框架来注册异常,而不是使用e.printStackTrace(); or System.err.println(e); System.err.println(e);

I personally recommend: http://www.slf4j.org/ , but if you have masochistic tendencies you can try the very bad (but official) Java Logging API: http://download.oracle.com/javase/1.4.2/docs/guide/util/logging/ . 我个人推荐: http//www.slf4j.org/ ,但如果你有自虐倾向,你可以尝试非常糟糕的(但官方的)Java Logging API: http//download.oracle.com/javase/1.4.2 / docs / guide / util / logging /

One extra advantage of SLF4J is that it can redirect its logging to the awful Java Logging API (that way you can use an elegantly designed API and still comform to the awfully designed (de jure not de facto) "standard" SLF4J的一个额外优势是它可以将其日志记录重定向到糟糕的Java Logging API(这样您就可以使用设计优雅的API,并且仍然符合设计得非常好(法律上不是事实上)的“标准”

SLF4J is easy to use, to log an exception all you have to do is write logger.error("some accompanying message", exception); SLF4J易于使用,记录异常所有你要做的就是写logger.error("some accompanying message", exception); , another of its advantages is that you can, for example, configure it to send you an email each time your application crashes (by using logback as the underlying logging engine) ,它的另一个优点是,例如,您可以将其配置为每次应用程序崩溃时向您发送电子邮件 (通过使用logback作为底层日志记录引擎)

It depends on the exceptions. 这取决于例外情况。 Obviously, with printStackTrace() the exception will be printed for you to debug (or users to report to you). 显然,使用printStackTrace()将打印异常以供您调试(或用户向您报告)。 However there is no additional error handling. 但是没有其他错误处理。

Example: If an IOException is thrown, you might want to show the user a error message specifying the exact error cause, or you might want to do another attempt, transparent for the user. 示例:如果抛出IOException,您可能希望向用户显示指定确切错误原因的错误消息,或者您可能希望对用户执行另一次透明操作。 Or you might want to abort the whole program if the operation is critical for the success of the whole task... etc. 或者如果操作对整个任务的成功至关重要,你可能想要中止整个程序......等等。

If you want to trace the source e.printStackTrace() is enough. 如果要跟踪源e.printStackTrace()就足够了。 Usually I put e.printStackTrace(); 通常我把e.printStackTrace(); at DEBUG level. 在DEBUG级别。 Also I add meaningful error message at ERROR level for the users. 此外,我在ERROR级别为用户添加有意义的错误消息。

I think you might be missing a bit about the basics of exceptions and exception handling. 我想你可能会忽略一些异常和异常处理的基础知识。

The golden rule of exceptions is that they should be exceptional. 例外的黄金法则是它们应该是例外的。

This is why you might have seen or read that you should never catch the base Exception - there is simply no way that your code can handle every time of exception. 这就是为什么你可能已经看到或者读过你应该永远不会捕获基本异常 - 你的代码根本无法处理每次异常。

So as a general rule you should only catch exceptions if you can handle them in a specific way. 因此,作为一般规则,如果您能够以特定方式处理异常,则应该只捕获异常。 For example, if you're reading a user's details from a file and that fails you might choose to return a new user. 例如,如果您正在从文件中读取用户的详细信息但失败则可能会选择返回新用户。 What you don't want to do is simply catch the exception and log it. 你不想做的只是捕获异常并记录它。 This leads to an application that is robust but simply swallows errors which leads to an extremely bad user experience. 这导致应用程序运行稳健,但只是吞下错误,导致极其糟糕的用户体验。

If your method can't handle an exception it should simply not catch it and defer the exception handling to a higher level. 如果您的方法无法处理异常,则应该不会捕获它并将异常处理推迟到更高级别。 This usually means an error message will be displayed to the user (at the top level). 这通常意味着将向用户显示错误消息(在顶层)。

If you can afford to use a logging framework like log4j , you'll be able to call 如果你能负担得起像log4j这样的日志框架,你就可以打电话了

}catch(Exception e){ log.error("Exception occurred:",e}

making the log framework to log your custom message "Exception occurred" followed by the stack trace in your errorlog file 使日志框架记录您的自定义消息“发生异常”,然后记录错误日志文件中的堆栈跟踪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM