Question

In Java bytecode, there is an option to specify the constant pool index 0 as the exception handler type, allowing you to catch all exceptions. However, as I understand it, this is exactly identical to catching Throwable. So what is the purpose of this feature? Did the designers of Java one day envision adding types of exceptions that don't inherit from Throwable? It doesn't look like it, but I can't think of another explanation apart from laziness.

Était-ce utile?

La solution

The JVM specification (§4.7.3) states:

If the value of the catch_type item is zero, this exception handler is called for all exceptions. This is used to implement finally (§3.13).

Although the use of catch_type zero is not specifically discussed in §3.13, the hint that it is used when compiling finally clauses in Java gives a clue. It may be that the JVM designers wanted a simple way to refer to all exception types without having to specifically refer to the Throwable class. The use of catch_type zero would be easier for the JVM to identify rather than having to index into the constant pool and resolve the java/lang/Throwable class name there.

In practical terms, as you say a catch_type zero or an index into the constant pool referring to java/lang/Throwable should work identically.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top