简体   繁体   English

捕获意外异常是一种好习惯吗?

[英]Is it good practice to catch unexpected exceptions?

I am writing a web application using Spring 3 framework. 我正在使用Spring 3框架编写Web应用程序。 Consider a situation where I have N number of controllers. 考虑一下我有N个控制器的情况。 Each controller delegates the request to exposed services and gets the response and return to users. 每个控制器将请求委派给公开的服务,并获得响应并返回给用户。

I would like to know if there is a better to way catch unexpected runtime exceptions thrown out of the controller code. 我想知道是否有更好的方法来捕获控制器代码抛出的意外运行时异常。 I do not want to write something like the following for each of my controller methods. 我不想为每个控制器方法编写类似以下内容的内容。 Or is it the only way? 还是唯一的方法?

try {
     //call the service
} catch(ServiceException serviceEx) {
    //do process for know exception  
} catch(Exception ex) {
    //return generic error message.
}

I know that we can use Spring Exception resolver. 我知道我们可以使用Spring Exception解析器。 But I do not want to show different error page when unexpected error happens. 但是当发生意外错误时,我不想显示其他错误页面。 I want to show some generic error message on UI as part of my widget? 我想在UI上显示一些通用错误消息作为小部件的一部分吗?

Edit: 编辑:

I also don't want my users to see the exception stacktrace when trying to perform some operations. 我也不想让我的用户在尝试执行某些操作时看到异常stacktrace。

Sundar 孙大信

I think catching RuntimeException s is bad practice. 我认为捕获RuntimeException是一个坏习惯。 Originally RuntimeException s where thought as inicators to programming errors (eg NullPointerException to indicate that a null check is missing). 最初是RuntimeException ,在其中被认为是编程错误的提示(例如, NullPointerException表示缺少null检查)。 Whereas checked exceptions where meant to indicate errors that the program can recover from (eg FileNotFoundException ). 而检查异常则表示程序可以从中恢复错误(例如FileNotFoundException )。

The problem today is that many frameworks use RuntimeException s where checked exceptions should be used. 今天的问题是,许多框架都使用RuntimeException ,其中应使用检查的异常。 Therefore it is difficult to differentiate between cases where a program can handle the exception and where a programming error (bug) is encountered. 因此,很难区分程序可以处理异常的情况和遇到编程错误(错误)的情况。

This is my personal view of this things for enterprise development. 这是我对企业发展这件事的个人看法。 I know that most people mandate for dropping checked exceptions and handle everything as unchecked exception (as in Scala). 我知道大多数人都要求删除受检查的异常,并将所有内容作为未检查的异常进行处理(例如在Scala中)。

In my opinion you should only catch exceptions in your code from which you can recover. 我认为您应该只在代码中捕获可以从中恢复的异常。 All other exceptions (checked or unchecked) should be caught by one über exceptionhandler which will log the exception and show the user some generic error page (possibly with an id that you can use to find what the exception was). 所有其他异常(选中或未选中)都应由一个über异常处理程序捕获,该异常处理程序将记录该异常并向用户显示一些常规错误页面(可能具有可用于查找异常的ID)。

For example, in struts2 you would that this like so: 例如,在struts2中,您希望这样:

<global-exception-mappings>
    <exception-mapping exception="java.lang.Exception" result="unrecoverableException"/>
</global-exception-mappings>

I never use spring mvc but this article seems to give you the options for an über exceptionhandler: 我从不使用spring mvc,但本文似乎为您提供über异常处理程序的选项:

http://doanduyhai.wordpress.com/2012/05/06/spring-mvc-part-v-exception-handling/ http://doanduyhai.wordpress.com/2012/05/06/spring-mvc-part-v-exception-handling/

You can catch runtime unexpected exceptions using this approach. 您可以使用这种方法捕获运行时意外异常。

  try {
  ...
  } catch ( Exception e ) {
  throw new RuntimeException("msg",e);
  }  

I think RuntimeException is designed for situations like breach of contract or some unrecoverable fault. 我认为RuntimeException是为诸如违约或某些无法恢复的错误之类的情况而设计的。 Most of the time, you should just let the container or some outer infrastructure to handle it for you. 大多数时候,您应该只让容器或某些外部基础结构为您处理它。 Or you have to write a lot of redundant code to deal with them and it's kind of against the design philosophy of Spring. 或者,您必须编写很多冗余代码来处理它们,这有点违背Spring的设计理念。

In your case, may be you can leverage some filter or interceptor to catch those unexpected RuntimeException if you really need to transfer them to some other generic messages. 就您而言,如果确实需要将它们转移到其他通用消息中,则可以利用某些过滤器或拦截器来捕获那些意外的RuntimeException。

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

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