Why do I need multi-catch blocks when Java exits the try block when the first error is encountered?

huangapple go评论60阅读模式
英文:

Why do I need multi-catch blocks when Java exits the try block when the first error is encountered?

问题

当运行时错误发生时,它会退出 try 块。它只会抛出一个异常并进入 catch 块。那么为什么我需要多个 catch 块?多重 catch 块在哪里使用?

另外,我还可以使用 Throwable 来捕获任何异常。这样可以吗?

英文:

When the run time error occurs it exits the try block. It will throw only one exception and enter the catch block. Then why do I need multiple catch blocks? Where are multi-catch blocks used?

Also, I can also use Throwable to catch any exception. Is that right?

答案1

得分: 1

'compiler' 并未执行这些操作;编译器不存在。异常是运行时现象。

你的问题似乎在询问运行时异常分派程序如何选择异常处理程序:它基于类型匹配。

当你说 '进入**the** catch 块' 时,那是不正确的。它选择第一个捕获块,其参数类型与实际抛出的异常兼容赋值。

如果你总是写 `catch (Exception ex)`,那么这将捕获任何异常,因为 `Exception` 是所有异常类型的超类,如果这是第一个这样的 catch 块,将不会考虑其他块。但这并不总是合适的异常处理风格;建议更加具体。

或许这是理解多个 catch 块的好方法:你在表达“如果是 X 类型的异常就执行这个;否则,如果是 Y 类型的异常就执行另一个操作;否则,如果是 Z 类型的异常就执行第三件事”。

顺便提一下,除非你完全了解通过这样做会干扰哪些机制,你很少会想要写 `catch (Throwable th)`。
英文:

The 'compiler' isn't doing any of this; the compiler is not around. Exceptions are a runtime phenomenon.

Your question appears to be asking how the runtime exception dispatcher picks an exception handler: it is based on type matching.

When you say 'enter the catch block', that is not correct. It selects the first catch-block whose argument type is assignment-compatible with the exception actually thrown.

If you always write catch (Exception ex) then this will catch any exception, since Exception is a superclass of all exception types, and if this is the first such catch block no other will be considered. But this is not always an appropriate exception-handling style; you may be advised to be more specific.

Perhaps this is a good way to understand multiple catch-blocks: you're saying "if it's an X-type exception then do this; otherwise if it's a Y-type exception then do this other thing; otherwise if it's a Z-type exception then a third thing".

FWIW, you rarely want to write catch (Throwable th) unless you're fully aware of the mechanisms you're interfering with by so doing.

答案2

得分: 0

你可以使用多重捕获块来捕获不同类型的异常。假设你有一个实现了Throwable的自定义异常MyCoolExn。

try {
    // 执行一段代码
} catch (MyCoolExn e) { // 捕获你的自定义异常
    System.out.println("发生了自定义异常");
} catch (Throwable e) { // 捕获其他任何异常
    System.out.println("发生了未知异常");
}

请注意,块的顺序很重要。如果交换它们,MyCoolExn将永远不会被捕获,因为Throwable块会捕获所有异常。

英文:

Well, you can use a multi catch block to in order to catch different types of exceptions. Let's say you have a custom exception MyCoolExn that implements Throwable.

try {
    // run piece of code
} catch (MyCoolExn e) { // catching your custom exception
    System.out.println("custom exception occurred");
} catch (Throwable e) { // catch any other exception
    System.out.println("an unknown exception occurred ");
}

Note that the order of the blocks is important. If you swap them, MyCoolExn will never be caught, as the Trowable block will catch everything

huangapple
  • 本文由 发表于 2020年8月22日 02:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63528228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定