Exception handling and interfaces in Java

穿精又带淫゛_ 提交于 2019-12-12 15:23:19

问题


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?

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.


回答1:


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.




回答2:


You could just use unchecked exceptions (i.e. - have your exceptions extend RuntimeException). That way you don't have to declare anything thrown, and implementations can throw anything they like (again, as long as they extend RuntimeException).




回答3:


Unless you're giving NodeException special functionality that does not exist in Exception and does exist in NodeAdditionException/NodeRemoveException/etc., you shouldn't be making a super-exception for your Nodes. The client caller will likely use catch(Exception ex){...} if they want a catch-all, anyway. If there isn't any extra functionality there, there is no need for NodeException.

If you do have shared functionality between various NodeException subclasses which doesn't exist in Exception, that's a different story, and you should definitely use a superclass.



来源:https://stackoverflow.com/questions/20107848/exception-handling-and-interfaces-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!