您如何以及在哪里定义自己的 Java 异常层次结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2683182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How and where do you define your own Exception hierarchy in Java?
提问by EugeneP
How and where do you define your own Exception hierarchy in Java?
您如何以及在哪里定义自己的 Java 异常层次结构?
My main question concerns package location where your Exception classes must be defined.
我的主要问题涉及必须定义异常类的包位置。
Do we create a special package for our exceptions and put all classes inside it?
我们是否为异常创建了一个特殊的包并将所有类放入其中?
采纳答案by Chris Dail
I use this as a general rule.
我将此用作一般规则。
- Where it makes sense, use a pre-defined Java exception. For example, if your code has some sort of I/O Error, it is fine to throw an IOException.
- Only use exception hierarchies if you need to differentiate between the two exceptions in a try/catch block. A lot of times it is perfectly fine to have a single component throw a single exception type with different messages for different errors. If the user cannot really do anything to handle the error specially, use the same generic exception class. If the user is able to handle them differently, that is when you should use a hierarchy.
- For hierarchies, do not make all exceptions from different components inherit from a base exception. There is no real reason to do this. If the consumer wants to catch anything, they can simply catch Exception.
- For package location, I put an Exception class with the code it relates to. So if I have a BusinessService in a package a.b.c, I have a a.b.c.BusinessException to go with it. I'm not a fan of putting all exceptions into an exceptions package. It just makes it hard to find.
- 在有意义的地方,使用预定义的 Java 异常。例如,如果您的代码有某种 I/O 错误,则抛出 IOException 是可以的。
- 如果您需要区分 try/catch 块中的两个异常,请仅使用异常层次结构。很多时候,让单个组件抛出具有不同错误消息的单个异常类型是完全没问题的。如果用户无法真正做任何事情来专门处理错误,请使用相同的通用异常类。如果用户能够以不同的方式处理它们,那么您应该使用层次结构。
- 对于层次结构,不要让来自不同组件的所有异常都继承自基本异常。没有真正的理由这样做。如果消费者想要捕捉任何东西,他们可以简单地捕捉异常。
- 对于包位置,我将一个 Exception 类与它相关的代码放在一起。因此,如果我在 abc 包中有一个 BusinessService,那么我有一个 abcBusinessException 来配合它。我不喜欢将所有异常放入异常包中。它只是让人很难找到。
回答by Jim Kiley
I put all of my custom exceptions into a com.company.project.exceptionpackage. I do this rather than putting them "close" to the locations where they crop up.
我将所有自定义异常放入一个com.company.project.exception包中。我这样做而不是将它们“靠近”它们出现的位置。
Here's my reasoning: If a given exception is only cropping up within one or two service classes somewhere, it may not be a general enough exception to deserve its own class. Only if I see a common theme popping up in multiple places will I go to the trouble of creating a custom Exception class. And if it is popping up in multiple places, then there's no logical package to "attach" it to, so an exception-specific package seems like the right way to go.
这是我的推理:如果给定的异常只出现在某处的一两个服务类中,它可能不是一个足够普遍的异常,值得拥有自己的类。只有当我看到一个共同的主题出现在多个地方时,我才会去创建一个自定义的 Exception 类。如果它在多个地方弹出,那么就没有逻辑包可以“附加”它,因此特定于异常的包似乎是正确的方法。
回答by Etienne
You can create your Exception classes wherever you want.
您可以在任何地方创建您的异常类。
The important thing is to extend an existing Exception class (java.lang.Throwablein fact). For instance java.lang.Exceptionor java.lang.RuntimeException. The first is a checked exception while extending RuntimeException will result in an unchecked exception; the differences between the two are detailed here.
重要的是扩展现有的 Exception 类(java.lang.Throwable实际上)。例如java.lang.Exception或java.lang.RuntimeException。第一个是已检查异常,而扩展 RuntimeException 会导致未检查异常;此处详细介绍了两者之间的差异。
回答by Syntactic
The language does not specify any requirements for what packages user-defined Exceptionclasses should be put in. As long as the class extends java.lang.Throwable, it can be thrown.
该语言没有规定用户定义的Exception类应该放在什么包中。只要类 extends java.lang.Throwable,就可以抛出。

