Java 什么时候应该使用 Throwable 而不是 new Exception?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/498217/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:23:41  来源:igfitidea点击:

When should Throwable be used instead of new Exception?

javaexceptionthrowable

提问by WolfmanDragon

Given: Throwableis Exception's superclass.

鉴于:ThrowableisException的超类。

When I read texts on writing your own 'exceptions', I see examples of Throwablebeing used in the catchblock and other texts show new Exception()being used in the catchblock. I have yet to see an explanation of when one should use each.

当我阅读有关编写自己的“例外”的文本时,我看到Throwablecatch块中使用的示例,而其他文本显示new Exception()catch块中使用。我还没有看到有关何时应该使用每个的解释。

My question is this, when should Throwablebe used and when should new Exception()be used?

我的问题是,什么时候应该Throwable使用,什么时候应该new Exception()使用?

Inside the catchor elseblock using either:

catchorelse块内使用:

throw throwable;

or

或者

throw new Exception();

采纳答案by Zach Scrivena

(from comments) The issue that brought this up is that I need to pass an 'exception' to a piece of code a coworker is building if a collection does not get built.

(来自评论)提出这个问题的问题是,如果未构建集合,我需要将“异常”传递给同事正在构建的一段代码。

In that case, you might want to throw a checked exception. You could throw an Exception, an appropriate existing subclass of it (except RuntimeExceptionand its subclasses which are unchecked), or a custom subclass of Exception(e.g. "CollectionBuildException"). See the Java Tutorial on Exceptionsto get up to speed with Java exceptions.

在这种情况下,您可能想要抛出一个检查异常。您可以抛出一个Exception,它的一个适当的现有子类(除了RuntimeException及其未选中的子类),或一个自定义子类Exception(例如“ CollectionBuildException”)。请参阅Java 异常教程以快速了解 Java 异常。

回答by Ray Hidayat

Always throw an Exception(never a Throwable). You generally don't catch Throwableeither, but you can. Throwable is the superclass to Exceptionand Error, so you would catch Throwableif you wanted to not only catch Exceptions but Errors, that's the point in having it. The thing is, Errors are generally things which a normal application wouldn't and shouldn't catch, so just use Exceptionunless you have a specific reason to use Throwable.

总是抛出一个Exception(从不抛出Throwable)。你通常也不会抓住Throwable,但你可以。Throwable 是Exceptionand的超类Error,因此Throwable如果您不仅想捕获Exceptions还想捕获s ,那么您会捕获Error,这就是拥有它的重点。问题是,Errors 通常是普通应用程序不会也不应该捕获的东西,所以Exception除非您有特定的理由使用Throwable.

回答by Michiel de Mare

Throwable is an interface, not a class.Two classes extend Throwable, Exception and Error.

Throwable 是一个接口,而不是一个类。两个类扩展了 Throwable、Exception 和 Error。

The rule is: be as specific as you can when catching exceptions - that means for example catching Exception instead of Throwable, and IOException instead of Exception.

规则是:在捕获异常时尽可能具体 - 这意味着例如捕获 Exception 而不是 Throwable,以及 IOException 而不是 Exception。

Don't catch Errors - errors are bugs. Fix the code instead.

不要捕捉错误 - 错误就是错误。改为修复代码。

If you have to catch absolutely everything, use "catch Throwable", but this is bad form.

如果您必须完全捕获所有内容,请使用“catch Throwable”,但这是不好的形式。

回答by MichaelD

You should not really catch an exception and throw a new one as general as "new Exception".

您不应该真正捕获异常并抛出一个像“新异常”一样通用的新异常。

Instead, if you wish to bubble up the exception just do the following:

相反,如果您想冒泡异常,只需执行以下操作:

try {
    // Do some stuff here
}
catch (DivideByZeroException e) {
    System.out.println("Can't divide by Zero!"); 
} 
catch (IndexOutOfRangeException e) { 
    // catch the exception 
    System.out.println("No matching element found.");
}
catch (Throwable e) {
    throw e; // rethrow the exception/error that occurred
}

It is not good practise, I believe, to catch an exception and throw a new exception instead of the one that was raised to your code block, unless you raise a useful custom exception that provides enough context to elude to the cause of the original exception.

我相信,捕捉异常并抛出一个新异常而不是引发到代码块的异常并不是一种好的做法,除非您提出一个有用的自定义异常,该异常提供了足够的上下文来逃避原始异常的原因.

回答by Bill K

As I heard it when java first came out, the theory was that Throwable might be used for transfer of control in other cases besides exceptions. I've never seen it used that way though (and that's probably a Very Good Thing).

正如我在 java 刚出现时听到的那样,理论上除了异常之外,Throwable 还可以用于其他情况下的控制转移。不过,我从未见过它以这种方式使用(这可能是一件非常好的事情)。

So just catch Exception (or better yet, a more fine-grained exception).

所以只需捕获异常(或者更好的是,一个更细粒度的异常)。

回答by mP.

Throwable is meant to be only caught by the container or main loop of your program. Most of the time catching stuff below Exception eg Error does not add much capability to a program, after all what can you possible do if a VirtualError other Errors are thrown. Not much except log and continue.

Throwable 只能被程序的容器或主循环捕获。大多数情况下,捕获 Exception 下的内容,例如 Error 并没有为程序增加太多功能,毕竟如果抛出 VirtualError 其他错误,您可以做什么。除了登录并继续之外没有太多。

回答by Michael Borgwardt

throw new Exception();is something you should neverdo in a catch block, but you may have to or want to do throw new SomeException(throwable);(preserving the full stack trace) instead of throw throwable;in order to conform to the API of your method, e.g. when it declares to throw SomeExceptionbut you're calling code that might throw an IOExceptionthat you don't want to add to you method's throwsclause.

throw new Exception();是你永远不应该在 catch 块中做的事情,但你可能不得不或想要做 throw new SomeException(throwable);(保留完整的堆栈跟踪)而不是throw throwable;为了符合你的方法的 API,例如当它声明抛出SomeException但你重新调用可能会抛出IOException您不想添加到方法throws子句中的代码。

The probably most common case is new RuntimeException(throwable);to avoid having a throwsclause altogether. Many people will tell you this is a horrible abuse because you should be using checked exceptions. IMO they are wrong and checked exceptions are a mistake in the Java language design that just results in ugly, unmaintainable code.

可能最常见的情况是完全new RuntimeException(throwable);避免使用throws子句。许多人会告诉你这是一种可怕的滥用,因为你应该使用受检异常。IMO 他们错了,检查异常是 Java 语言设计中的一个错误,它只会导致丑陋、不可维护的代码。

回答by mP.

All exceptions are a problem in the end... too say Errors are bugs doesnt mean anything.

所有的异常最终都是一个问题......也说错误是错误并不意味着什么。

Errors are not bugs - they are problems that the host VM is experiencing, eg OutOfMemoryError. Exceptions are a means that the current operation may use to notify that it failed and perhaps provide some diagnosis.

错误不是错误 - 它们是主机 VM 遇到的问题,例如 OutOfMemoryError。异常是当前操作可以用来通知它失败并可能提供一些诊断的一种方式。

回答by mP.

Generally, you would not throw or catch Throwable. In particular, JVM errors (that extend Error() ) are not meantto be caught by user code unless you are doing weird system-level work.

通常,您不会抛出或捕获 Throwable。特别是,JVM错误(即延长错误())并不意味着,除非你正在做奇怪的系统级工作可能由用户代码捕获。

Treat "Throwable" as a language artifact. The "Exception" class is named that because it is the one that is intended to be used by programmers when they want a code block to exit "exceptionally" - by not exiting normally or returning a value.

将“Throwable”视为语言工件。之所以命名“Exception”类,是因为当程序员希望代码块“异常”退出时(通过不正常退出或返回值),他们打算使用该类。

That includes both regular error situations (by "regular" I mean as opposed to JVM errors) and places where you are using exceptions as a control mechanism.

这包括常规错误情况(我的意思是“常规”错误,而不是 JVM 错误)和使用异常作为控制机制的地方。

回答by Adrian

You shouldn't use Exceptions as a "return type" either...

您也不应该将 Exceptions 用作“返回类型”...

If the condition you're throwing for is common, you are spending a huge amount of resources to return that condition to the calling routine. Exceptions are expensive to construct.

如果您抛出的条件很常见,那么您将花费大量资源将该条件返回给调用例程。异常的构建成本很高。

I've seen cases where tight loops that threw exceptions as a "negative" for e.g. ID allocation, this routine occupied about 99% of the CPU time .. when changed to a documented return constant instead, this dropped to 25%.

我见过一些情况,其中紧密循环将异常作为“负面”抛出异常,例如 ID 分配,该例程占用了大约 99% 的 CPU 时间……当改为记录的返回常量时,这一比例下降到 25%。