📄 11.doc.html
字号:
the exception is thrown must appear to have taken place. No expressions, statements,
or parts thereof that occur after the point from which the exception is
thrown may appear to have been evaluated. If optimized code has speculatively
executed some of the expressions or statements which follow the point at which
the exception occurs, such code must be prepared to hide this speculative execution
from the user-visible state of the Java program.
<p><a name="44202"></a>
<h3>11.3.2 Handling Asynchronous Exceptions</h3>
<a name="44203"></a>
Most exceptions in Java occur synchronously as a result of an action by the thread
in which they occur, and at a point in the Java program that is specified to possibly
result in such an exception. An asynchronous exception is, by contrast, an exception
that can potentially occur at any point in the execution of a Java program.
<p><a name="47137"></a>
Asynchronous exceptions are rare in Java. They occur only as a result of:<p>
<ul><a name="44205"></a>
<li>An invocation of the <code>stop</code> methods of class <code>Thread</code> (<a href="javalang.doc18.html#8095">§20.20.15</a>, <a href="javalang.doc18.html#8096">§20.20.16</a>) or <code>ThreadGroup</code> (<a href="javalang.doc19.html#2885">§20.21.8</a>, <a href="javalang.doc19.html#2886">§20.21.9</a>)
<a name="44209"></a>
<li>An <code>InternalError</code> <a href="11.doc.html#44395">(§11.5.2.2)</a> in the Java Virtual Machine
</ul><a name="44210"></a>
The <code>stop</code> methods may be invoked by one thread to affect another thread or all the
threads in a specified thread group. They are asynchronous because they may
occur at any point in the execution of the other thread or threads. An
<code>InternalError</code>  is considered asynchronous so that it may be handled using the
same mechanism that handles the <code>stop</code> method, as will now be described.
<p><a name="44211"></a>
Java permits a small but bounded amount of execution to occur before an asynchronous exception is thrown. This delay is permitted to allow optimized code to detect and throw these exceptions at points where it is practical to handle them while obeying the semantics of the Java language.<p>
<a name="44212"></a>
A simple implementation might poll for asynchronous exceptions at the point of each control transfer instruction. Since a Java program has a finite size, this provides a bound on the total delay in detecting an asynchronous exception. Since no asynchronous exception will occur between control transfers, the code generator has some flexibility to reorder computation between control transfers for greater performance.<p>
<a name="46637"></a>
The paper <i>Polling Efficiently on Stock Hardware </i>by Mark Feeley, <i>Proc. 1993 Conference on Functional Programming and Computer Architecture</i>, Copenhagen, Denmark, pp. 179-187, is recommended as further reading.<p>
<a name="44216"></a>
Like all exceptions, asynchronous exceptions are precise <a href="11.doc.html#44199">(§11.3.1)</a>.<p>
<a name="44218"></a>
<h2>11.4 An Example of Exceptions</h2>
<a name="44219"></a>
Consider the following example:
<p><pre><a name="46616"></a>
class TestException extends Exception {
<br><a name="46618"></a> TestException() { super(); }
<br><br><a name="44223"></a> TestException(String s) { super(s); }
<br><a name="44224"></a>}
<a name="46659"></a>
class Test {
<a name="44226"></a>
public static void main(String[] args) {
<a name="44227"></a> for (int i = 0; i < args.length; i++) {
</pre><pre><a name="46972"></a>
try {
<a name="44229"></a> thrower(args[i]);
<a name="44230"></a> System.out.println("Test \"" + args[i] +
<a name="44231"></a> "\" didn't throw an exception");
<a name="44232"></a> } catch (Exception e) {
<a name="44233"></a> System.out.println("Test \"" + args[i] +
<a name="44234"></a> "\" threw a " + e.getClass() +
<a name="44235"></a> "\n        with message: " + e.getMessage());
<a name="44236"></a> }
<a name="44237"></a> }
<a name="44238"></a> }
<br><a name="44239"></a>
static int thrower(String s) throws TestException {
<a name="44240"></a> try {
<a name="44241"></a> if (s.equals("divide")) {
<a name="44242"></a> int i = 0;
<a name="44243"></a> return i/i;
<a name="44244"></a> }
<a name="44245"></a> if (s.equals("null")) {
<a name="44246"></a> s = null;
<a name="44247"></a> return s.length();
<a name="44248"></a> }
<a name="44249"></a> if (s.equals("test"))
<a name="44250"></a> throw new TestException("Test message");
<a name="44251"></a> return 0;
<a name="44252"></a> } finally {
<a name="44253"></a> System.out.println("[thrower(\"" + s +
<a name="46660"></a> "\") done]");
<a name="44254"></a> }
<a name="44255"></a> }
<a name="44256"></a>}
</pre><a name="44257"></a>
If we execute the test program, passing it the arguments:
<p><pre><a name="44258"></a>divide null not test
</pre><a name="44259"></a>
it produces the output:
<p><pre><a name="44260"></a>
[thrower("divide") done]
<a name="44261"></a>Test "divide" threw a class java.lang.ArithmeticException
<a name="44262"></a>        with message: / by zero
<a name="44263"></a>[thrower("null") done]
<a name="44264"></a>Test "null" threw a class java.lang.NullPointerException
<a name="44265"></a>        with message: null
<a name="44266"></a>[thrower("not") done]
<a name="44267"></a>Test "not" didn't throw an exception
<a name="44268"></a>[thrower("test") done]
<a name="44269"></a>Test "test" threw a class TestException
<a name="44270"></a>        with message: Test message
</pre><a name="44271"></a>
This example declares an exception class <code>TestException</code>. The <code>main</code> method of class <code>Test</code> invokes the <code>thrower</code> method four times, causing exceptions to be thrown three of the four times. The <code>try</code> statement in method <code>main</code> catches each exception that the <code>thrower</code> throws. Whether the invocation of <code>thrower</code> completes normally or abruptly, a message is printed describing what happened.<p>
<a name="44272"></a>
The declaration of the method <code>thrower</code> must have a <code>throws</code> clause because it  can throw instances of <code>TestException</code>, which is a checked exception class <a href="11.doc.html#44121">(§11.2)</a>. A compile-time error would occur if the <code>throws</code> clause were omitted.<p>
<a name="44276"></a>
Notice that the <code>finally</code> clause is executed on every invocation of <code>thrower</code>, whether or not an exception occurs, as shown by the "<code>[thrower(</code>...<code>) done]</code>" output that occurs for each invocation<p>
<a name="44278"></a>
<h2>11.5 The Exception Hierarchy</h2>
<a name="44279"></a>
The possible exceptions in a Java program are organized in a hierarchy of classes,
rooted at class <code>Throwable</code> (<a href="11.doc.html#44278">§11.5</a>, <a href="javalang.doc20.html#46198">§20.22</a>), a direct subclass of <code>Object</code>. The
classes <code>Exception</code> and <code>Error</code> are direct subclasses of <code>Throwable</code>. The class
<code>RuntimeException</code> is a direct subclass of <code>Exception</code>.
<p><a name="55113"></a>
The exception classes declared by the standard packages <code>java.lang</code>, <code>java.util</code>, <code>java.io</code> and <code>java.net</code> are called the <i>standard exception classes</i>.<p>
<a name="55114"></a>
Java programs can use the pre-existing exception classes in <code>throw</code> statements, or define additional exception classes, as subclasses of <code>Throwable</code> or of any of its subclasses, as appropriate. To take advantage of Java's compile-time checking for exception handlers, it is typical to define most new exception classes as checked exception classes, specifically as subclasses of <code>Exception</code> that are not subclasses of <code>RuntimeException</code>.<p>
<a name="44285"></a>
<h3>11.5.1 The Classes <code>Exception</code> and <code>RuntimeException</code></h3>
<a name="44286"></a>
The class <code>Exception</code> is the superclass of all the exceptions that ordinary programs
may wish to recover from.
<p><a name="44287"></a>
<h4>11.5.1.1 Standard Runtime Exceptions</h4>
<a name="44288"></a>
The class <code>RuntimeException</code> is a subclass of class <code>Exception</code>. The subclasses
of <code>RuntimeException</code> are unchecked exception classes.
<p><a name="44289"></a>
Package <code>java.lang</code> defines the following standard unchecked runtime exceptions, which, like all other classes in package <code>java.lang</code>, are implicitly imported and therefore may be referred to by their simple names:<p>
<ul><a name="44290"></a>
<li><code>ArithmeticException</code>: An exceptional arithmetic situation has arisen, such as an integer division <a href="15.doc.html#5047">(§15.16.2)</a> operation with a zero divisor.
<a name="44297"></a>
<li><code>ArrayStoreException</code>: An attempt has been made to store into an array component a value whose class is not assignment compatible with the component type of the array (<a href="10.doc.html#11430">§10.10</a>, <a href="15.doc.html#5295">§15.25.1</a>).
<a name="44304"></a>
<li><code>ClassCastException</code>: An attempt has been made to cast (<a href="5.doc.html#176921">§5.4</a>, <a href="15.doc.html#238146">§15.15</a>) a reference to an object to an inappropriate type.
<a name="44305"></a>
<li><code>IllegalArgumentException</code>: A method was passed an invalid or inappropriate argument or invoked on an inappropriate object. Subclasses of this class include:
<ul>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -