将异常作为方法参数处理 [英] Handling an exception as a method argument

查看:97
本文介绍了将异常作为方法参数处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种设计模式来处理作为方法参数接收的 Exception 实例。



放置一些在问题的上下文中,我正在使用GWT并具有各种异步处理程序,这些处理程序通常以类似于以下形式的形式出现:

 公共接口AsyncCallback< ; T> {

无效onFailure(Throwable捕获);

onSuccess无效(T结果);

}

因此, onFailure 方法收到一个我需要处理的 Throwable 实例。



现在,我有一个数字我可以用这种方法收到的异常,例如




  • ConstraintViolationException

  • TimeoutException

  • NoSuchElementException



在我的处理代码中,我当然可以编写以下内容:

  void handleException(final Exception e){
if(e instanceof TimeoutException){
handleTimeout();
}否则,如果(e instanceof NoSuchElementException){
handleInvalidElement();
} else {
stopAndCatchFire();
}
}

但是在我看来,这看起来很丑。大型的 if..else if 链和大量使用 instanceof 的组合似乎是应该避免的东西。



我认为也许我可以使用 try ... catch 构造来处理 Exception 使用以下内容:

  void handleException(final Exception e){
try {
投掷e;
} catch(TimeoutException te){
handleTimeout();
} catch(NoSuchElementException nsee){
handleInvalidElement();
} catch(异常例外){
stopAndCatchFire();
}
}

但这似乎是一种滥用。您是否看到第二种方法或其他可以避免第一种情况的方法的缺点?

解决方案

有一个exceptionHandlers字典,由它们处理的异常类型作为关键字,然后当您获取异常时,您会在字典中查找该异常类型的处理程序。如果存在,则将异常传递给处理程序,如果不存在,则使用默认处理程序。



这样,您的处理程序将变成这样:

  void handleException(final Exception e){
if(handlers.containsKey(e.getType())
{
handlers [e.getType()]。handle(e);
}
else
{
defaultHandler.handle(e);
}
}

我的Java有点生锈,因此示例是c-sharpy但应该



这种方法的优点是可以简单地添加新的处理程序。



p>

但是如果您为子类型使用相同的处理程序,则会受到影响,因为您必须显式注册每个子类型。



<为了解决这个问题,只需让每个处理程序负责决定是否可以处理n例外:

 公共接口ExceptionHandler 
{
bool canHandle(Exception e);
无效句柄(异常e)
}

然后将处理程序放入列出一个反复询问每个对象是否可以处理当前异常的列表,当您找到可以处理的异常时,请使其进行处理。


I am looking for a design pattern to handle Exception instances received as method arguments.

To put some context into the question, I am using GWT and have various asynchronous handlers that usually come in a form similar to:

public interface AsyncCallback<T> {

  void onFailure(Throwable caught);

  void onSuccess(T result);

}

So, the onFailure method receives a Throwable instance that I need to handle.

Now, I have a number of exceptions I can receive in this method, for example

  • ConstraintViolationException
  • TimeoutException
  • NoSuchElementException

In my handling code I could of course write the following:

void handleException(final Exception e) {
    if(e instanceof TimeoutException) {
        handleTimeout();
    } else if (e instanceof NoSuchElementException) {
        handleInvalidElement();
    } else {
        stopAndCatchFire();
    }
}

But to my eye, that looks very ugly. The large if..else if chain combined with heavy usage of instanceof seems like something that should be avoided.

I thought that maybe I could use the try...catch construct to handle the Exception using something like the following:

void handleException(final Exception e) {
    try {
        throw e;
    } catch (TimeoutException te) {
        handleTimeout();
    } catch (NoSuchElementException nsee) {
        handleInvalidElement();
    } catch (Exception ex) {
        stopAndCatchFire();
    }
}

But this seems like an abuse somehow. Do you see any downsides to the second approach or another approach I could go with to avoid the first case?

解决方案

Could you not have a dictionary of exceptionHandlers keyed by the type of exception they handle, then when you get a exception you look in the dictionary for the handler for the exception type. If there is one, then pass the exception to the handler, if there isn't then use the default handler.

So your handler becomes something like this:

void handleException(final Exception e) {
    if (handlers.containsKey(e.getType())
    {
        handlers[e.getType()].handle(e);
    }
    else
    {
         defaultHandler.handle(e);
    }
}

My Java is a bit rusty, so the example is c-sharpy but should be simple enough to translate (though I remembered not to capitalise the first letter of everything :))

This approach should have the advantage that you can add new handlers simply.

It will however suffer if you have the same handler for sub types, as you will have to register each subtype explicitly.

To get around this issue simply make each handler responsible for making the decision about whether it can handle an exception:

public interface ExceptionHandler
{
     bool canHandle(Exception e);
     void handle(Exception e)
}

then just put the handlers in a list an iterate asking each one if it can handle the current exception and when you find one that can, get it to handle it.

这篇关于将异常作为方法参数处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆