Java中的异常处理和接口 [英] Exception handling and interfaces in Java

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

问题描述

我已经编写了一个接口和一个实现,我无法真正决定处理以下问题的最佳方法。为了缩短它,我们假设这是接口:

I have written an interface and an implementation to it and I can't really decide on the best way to handle the following problem. To make it short, let's say this is the interface:

public interface Node {
    String getNodeName();
    Node getChildByName(String name);
    void addChild(Node node);
    void removeChild(Node node);
}

现在,如果我想处理异常,最好是制作异常尽可能通用或者我应该添加相当具体的异常吗?

Now if I wanted to handle exceptions, is it a good idea to make the exception as generic as possible or should I add rather specific ones?

假设方法 addChild ,它应该抛出一个可能存在的泛型NodeException在任何其他方法中使用,以便实现类可以按需扩展它,或者是否可以使用只有这些方法应该抛出的NodeAdditionException?一般异常的优点是,如果我在内部使用其中一个方法并抛出异常,我不需要包装异常,我可以让它上升到堆栈直到有人抓住它。它通常会转到我的实现调用者,然后他需要处理它。调用者可以捕获泛型异常以仅使用一个例程处理所有异常或捕获任何子类。这同时是一个巨大的缺点,因为调用者可能会捕获许多不同的子异常,甚至不需要在当前实现中使用。这是一种交易破坏者。解决这个问题的一种方法是为要使用的实现创建一组固定的异常,这也不是我想要做的。

Let's say the method addChild, should it throw a generic NodeException that could potentially be used in any other method so that the implementing class can extend it on demand, or is it okay to go with a NodeAdditionException that only this methods should throw? The advantage of a generic exception would be that I don't need to wrap exceptions if I use one of the methods internally and an exception is throws, I can just let it go up the stack until someone catches it. It will usually go up to the caller of my implementation and he needs to deal with it then. The caller can catch the generic exception to handle all the exceptions with just one routine or catch any of the child classes. This is at the same time a huge disadvantage, as the caller potentially could catch many different child exceptions that don't even need to be used in the current implementation. That is kind of a deal breaker. A way to solve this is to create a set of fixed exceptions for the implementation to be used which also isn't what I would want to do.

另一种方式是是具体的例外,但这也是有代价的。你可能会抛出一组固定的异常,这就是它的优点,但是如果你在内部使用任何方法,你需要捕获异常,将其包装成一个新异常并再次抛出。如果我做对了,这是非常重的资源,也是很多锅炉板代码。最后我会把它当作处理异常的更干净的方法。

The other way would be the specific exception but this also comes at a price. You would have a fixed set of exception that can be thrown and that's it, an advantage, but if you use any method internally you need to catch the exception, wrap it up in a new one and throw it again. If I got it right, this is quite resource heavy and it's a lot of boiler plate code to write, too. In the end I would take this as the cleaner method to handle exception.

还有其他方法可以处理异常,但这是我现在无法想到的我是否忽略了这两者的重要细节,还是我需要选择这两种罪恶中的较小者?任何帮助表示赞赏。

Is there any other way to handle exceptions but this that I just can't think off right now, have I overlooked an important detail on these two or do I need to chose the lesser of these two evils? Any help appreciated.

推荐答案

在您的例外中有一个继承结构;例如, NodeAddException扩展NodeException 。这样,异常的捕获者可以像它需要的那样具体或一般。

Have an inheritance structure in your exceptions; for example, NodeAddException extends NodeException. That way, the catcher of an exception can be as specific or as general as it needs to be.

这篇关于Java中的异常处理和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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