java 自定义异常类 - 从异常或 Thowable 扩展?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14891717/
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
Custom Exception class - extends from Exception or Thowable?
提问by Jay
I am designing a custom Exception class for my application. I have a very basic question. Should I extend from Exception classor Thowable class? What are the benefits ?
我正在为我的应用程序设计一个自定义 Exception 类。我有一个非常基本的问题。我应该从Exception 类还是Thowable 类扩展?有什么好处?
I intend to throw this from underlying layers and catch it in the top level classes. Will it influence my decision of using Thowable over Exception. Is it fundamentally right to catch a Thowable ?
我打算从底层抛出它并在顶级类中捕获它。它会影响我使用 Thowable 而不是 Exception 的决定。捕获 Thowable 从根本上是正确的吗?
I 've gone through some other threads in this forum. They talk about having the stack trace maintained when it is thrown and not having it for exception etc. I understand that some say ( here) that Thowable is super class of Exception and we should not use it. But others (here) say Exception is for "Exceptional " cases.
我在这个论坛上浏览了一些其他主题。他们谈论在抛出堆栈跟踪时维护堆栈跟踪而不是异常等。我理解有人说(这里)Thowable 是异常的超类,我们不应该使用它。但是其他人(这里)说 Exception 是针对“Exceptional”的情况。
This question is rather a discussion of how one is better than other rather than asking how.
这个问题更像是讨论一个人如何比其他人更好,而不是问如何。
回答by Ostap Andrusiv
Throwable
is a class for all the bad situations, which can arise: Errors & Exceptions.
Throwable
是所有可能出现的不良情况的类:错误和异常。
Error
is something, you can't handle at all: OutOfMemoryError
, VirtualMachineError
, etc.
Error
是什么东西,你不能在所有的处理:OutOfMemoryError
,VirtualMachineError
,等。
Exception
is for exceptional cases.
Exception
用于特殊情况。
Exceptionscome in 2 flavours:
例外情况有两种:
RuntimeException
s.These ones, you are not aware of:
NullPointerException
,ClassCastException
, etc.Checked
exceptions.These are the exceptions, which your code is aware ofand should be explicitely catched (
... throws MyException
):IOException
s, etc.
RuntimeException
s。这些的,你是不知道的:
NullPointerException
,ClassCastException
等等。Checked
例外。这些是您的代码知道并且应该明确捕获的异常(
... throws MyException
):IOException
s 等。
If you want the users of your code, to explicitely handle some exceptional situations, it would be good to just extend Exception
, not the RuntimeException
. There's no need to extend Throwable
.
如果您希望代码的用户明确地处理一些特殊情况,最好只扩展Exception
,而不是RuntimeException
. 没有必要扩展Throwable
。
回答by Subhrajyoti Majumder
Fundamentally you should extends Exception
class as you are creating Custom Exception
. Exception
and Error
both extends Throwable
, It really does not make sense extending Throwable
.
从根本上说,您应该Exception
在创建Custom Exception
. Exception
并且Error
都扩展了Throwable
,扩展真的没有意义Throwable
。
回答by RAS
Throwable
is the super class of Error
& Exception
.
Throwable
是Error
&的超类Exception
。
Like Exception
, Error
too, can be thrown & handled.
象Exception
,Error
也可以抛出与处理。
But it is not advisable, according to the following doc:
但根据以下文档,这是不可取的:
You are not required to catch Error objects or Error subtypes. You can also throw an Error yourself (although other than AssertionError you probably won't ever want to), and you can catch one, but again, you probably won't. What, for example, would you actually do if you got an OutOfMemoryError?
您不需要捕获 Error 对象或 Error 子类型。你也可以自己抛出一个错误(尽管除了 AssertionError 你可能永远不会想要),你可以抓住一个,但同样,你可能不会。例如,如果您遇到 OutOfMemoryError,您实际上会怎么做?
Keeping this concept in mind, I would suggest to extend Throwable
if you want to throw and/or catch Exception
& Error
both. Extend Exception
if you want to throw and/or catch Exception
only.
牢记这个概念,Throwable
如果你想投掷和/或捕捉Exception
和Error
两者兼而有之,我建议扩展。Exception
如果您只想投掷和/或捕捉,请扩展Exception
。