java throws Throwable 和 throws Exception 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5618082/
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
What is the difference between throws Throwable and throws Exception
提问by user496949
Any difference about those two?
这两者有什么区别吗?
回答by Hendrik Brummermann
Exceptions are for errors in the program logic. Error are used by the JVM to signal that something is wrong with the environment such as an OutOfMemoryError or IncompatibleClassChangeError. ThreadDeath is used to kill Threads. Throwable is a super class over all of these.
例外是程序逻辑中的错误。JVM 使用错误来表示环境有问题,例如 OutOfMemoryError 或 IncompatibleClassChangeError。ThreadDeath 用于杀死线程。Throwable 是所有这些的超类。
In normal program logic, you should never throw nor catch Throwables or Errors. The main reason I can think of for catching a Errors is this: You are using your own class loading system and want to isolate broken plugins.
在正常的程序逻辑中,您永远不应该抛出或捕获 Throwables 或 Errors。我能想到的捕获错误的主要原因是:您正在使用自己的类加载系统并希望隔离损坏的插件。
The JavaDoc for ThreadDeath for example says:
例如,ThreadDeath 的 JavaDoc 说:
An application should catch instances of this class only if it must clean up after being terminated asynchronously. If ThreadDeath is caught by a method, it is important that it be rethrown so that the thread actually dies.
仅当应用程序在异步终止后必须进行清理时,它才应捕获此类的实例。如果 ThreadDeath 被方法捕获,重要的是将其重新抛出,以便线程实际死亡。
回答by Christina
The Throwable class is extended both by the Exception class and the Error class, so by using throws Throwable you are actually saying that your method may not only throw an Exception but also an Error. The difference between the two according to the documentation is that Exceptions are
Throwable 类由 Exception 类和 Error 类扩展,因此通过使用 throws Throwable,您实际上是在说您的方法不仅可以抛出异常,还可以抛出错误。根据文档,两者之间的区别在于 Exceptions 是
conditions that a reasonable application might want to catch
合理的应用程序可能想要捕捉的条件
while errors indicate
而错误表明
serious problems that a reasonable application should not try to catch
合理的应用程序不应试图捕捉的严重问题
See herefor more details.
请参阅此处了解更多详情。
回答by Nishant
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.
Instances of two subclasses, Error and Exception, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).
Throwable 类是 Java 语言中所有错误和异常的超类。只有属于此类(或其子类之一)的实例的对象才会被 Java 虚拟机抛出或可以被 Java throw 语句抛出。同样,只有此类或其子类之一可以是 catch 子句中的参数类型。
两个子类的实例,Error 和 Exception,通常用于指示发生了异常情况。通常,这些实例是在异常情况的上下文中新创建的,以便包含相关信息(例如堆栈跟踪数据)。
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
Exception 类及其子类是 Throwable 的一种形式,它指示合理的应用程序可能想要捕获的条件。
Please read the doc, it explains.
请阅读文档,它解释了。
回答by Jigar Joshi
This is the hierarchy
这是层次结构
java.lang.Object
java.lang.Throwable
java.lang.Exception
回答by Tommy Siu
A Throwable could be a Error or Exception.
Throwable 可能是错误或异常。
From Java doc:
来自 Java 文档:
An Error
is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
AnError
是 Throwable 的子类,表示合理的应用程序不应尝试捕获的严重问题。大多数此类错误是异常情况。
The class Exception
and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
该类Exception
及其子类是 Throwable 的一种形式,它指示合理的应用程序可能想要捕获的条件。
回答by Pa?lo Ebermann
If a Throwable
could only be an Error
or an Exception
, there would be no difference between throws Throwable
and throws Exception
for a method, as Error
can be always thrown, even if it was not declared (like RuntimeException
, too).
如果 aThrowable
只能是 anError
或 an Exception
,则方法throws Throwable
和throws Exception
方法之间没有区别,因为Error
总是可以抛出,即使它没有声明(例如RuntimeException
,也是)。
But a method declared with throws Throwable
can also use throw new Throwable("example");
, or something like this:
但是声明为 with 的方法throws Throwable
也可以使用throw new Throwable("example");
,或类似的东西:
class CannonBall extends Throwable {}
throw new CannonBall();
There normally is no reason to do something like this, so in practice throws Throwable
and throws Exception
areequivalent.
通常没有理由做这样的事情,所以在实践中throws Throwable
和throws Exception
是等价的。