⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javalang.doc20.html

📁 java语言规范
💻 HTML
字号:
<html>
<head>
<title>The Java Language Specification The Package java.lang </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
 
<a href="index.html">Contents</a> | <a href="javalang.doc19.html">Prev</a> | <a href="javalang.doc21.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="46198"></a>
<center><h1>20.22  The Class  <code>java.lang.Throwable</code> and its Subclasses</h1></center>
<a name="46202"></a>
The <code>throw</code> statement <a href="14.doc.html#237350">(&#167;14.16)</a> is permitted to throw only instances of the class 
<code>Throwable</code> and its subclasses. Instances of two subclasses, <code>Error</code> and <code>Exception
</code>, 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).
<p><a name="27731"></a>
The following list shows the hierarchical relationships of all the exception classes predefined in package <code>java.lang</code> by the Java language:<p>
<pre><a name="14482"></a>
<code><b>Throwable
</b></code><a name="14483"></a><code><b>	Error
</b></code><a name="14484"></a><code><b>		LinkageError
</b></code><a name="14485"></a><code><b>			ClassCircularityError
</b></code><a name="14486"></a><code><b>			ClassFormatError
</b></code><a name="14487"></a><code><b>			ExceptionInInitializerError<br>
			IncompatibleClassChangeError
</b></code><a name="14488"></a><code><b>				AbstractMethodError
</b></code><a name="14489"></a><code><b>				IllegalAccessError
</b></code><a name="14490"></a><code><b>				InstantiationError
</b></code><a name="14491"></a><code><b>				NoSuchFieldError
</b></code><a name="14492"></a><code><b>				NoSuchMethodError
</b></code><a name="14493"></a><code><b>			NoClassDefFoundError
</b></code><a name="14494"></a><code><b>			UnsatisfiedLinkError
</b></code><a name="14495"></a><code><b>			VerifyError
</b></code><a name="14497"></a><code><b>		VirtualMachineError
</b></code><a name="14498"></a><code><b>			InternalError
</b></code><a name="14499"></a><code><b>			OutOfMemoryError
</b></code><a name="14500"></a><code><b>			StackOverflowError
</b></code><a name="14501"></a><code><b>			UnknownError
</b></code><a name="2910"></a><code><b>		ThreadDeath
</b></code><a name="14502"></a><code><b>	Exception
</b></code><a name="30943"></a><code><b>		ClassNotFoundException
</b></code><a name="30945"></a><code><b>		CloneNotSupportedException
</b></code><a name="14504"></a><code><b>		IllegalAccessException
</b></code><a name="14505"></a><code><b>		InstantiationException
</b></code><a name="14506"></a><code><b>		InterruptedException
</b></code><a name="14508"></a><code><b>		RuntimeException
</b></code><a name="14509"></a><code><b>			ArithmeticException
</b></code><a name="14510"></a><code><b>			ArrayStoreException
</b></code><a name="14511"></a><code><b>			ClassCastException
</b></code><a name="36581"></a><code><b>			IllegalArgumentException
</b></code><a name="36583"></a><code><b>				IllegalThreadStateException
</b></code><a name="36916"></a><code><b>				NumberFormatException
</b></code><a name="36920"></a><code><b>			IllegalMonitorStateException
</b></code><a name="36917"></a><code><b>			IndexOutOfBoundsException
</b></code><a name="36918"></a><code><b>			NegativeArraySizeException
</b></code><a name="14519"></a><code><b>			NullPointerException
</b></code><a name="14520"></a><code><b>			SecurityException
</b></code></pre><a name="27737"></a>
By convention, class <code>Throwable</code> and all its subclasses have two constructors, one that takes no arguments and one that takes a <code>String</code> argument that can be used to produce an error message. This is true of all the classes shown above, with one exception: <code>ExceptionInInitializerError</code>. These predefined classes otherwise have no new content; they merely inherit methods from class <code>Throwable</code>.<p>
<pre><a name="3262"></a>public class <code><b>Throwable</b></code> {
<a name="3263"></a>	public <code><b>Throwable</b></code>();
<a name="3264"></a>	public <code><b>Throwable</b></code>(String message);
<a name="3302"></a>	public String <code><b>toString</b></code>();
<a name="3265"></a>	public String <code><b>getMessage</b></code>();
<a name="3268"></a>	public Throwable <code><b>fillInStackTrace</b></code>();
<a name="27845"></a>	public void <code><b>printStackTrace</b></code>();
<a name="27849"></a>	public void <code><b>printStackTrace</b></code>(java.io.PrintStream s);
<a name="3294"></a>}
</pre><a name="3273"></a>
<p><font size=+1><strong>20.22.1   </strong> <code>public <code><b>Throwable</b></code>()</code></font>
<p>
<a name="27744"></a>
This constructor initializes a newly created <code>Throwable</code> object with <code>null</code> as its 
error message string. Also, the method <code>fillInStackTrace</code> <a href="javalang.doc20.html#10057">(&#167;20.22.5)</a> is called 
for this object.
<p><a name="3274"></a>
<p><font size=+1><strong>20.22.2   </strong> <code>public <code><b>Throwable</b></code>(String message)</code></font>
<p>
<a name="27833"></a>
This constructor initializes a newly created <code>Throwable</code> object by saving a reference
to the error message string <code>s</code> for later retrieval by the <code>getMessage</code> method 
<a href="javalang.doc20.html#3275">(&#167;20.22.3)</a>. Also, the method <code>fillInStackTrace</code> <a href="javalang.doc20.html#10057">(&#167;20.22.5)</a> is called for this 
object.
<p><a name="3275"></a>
<p><font size=+1><strong>20.22.3   </strong> <code>public String <code><b>getMessage</b></code>()</code></font>
<p>
<a name="27763"></a>
If this <code>Throwable</code> object was created with an error message string <a href="javalang.doc20.html#3274">(&#167;20.22.2)</a>, 
then a reference to that string is returned.
<p><a name="27767"></a>
If this <code>Throwable</code> object was created with no error message string <a href="javalang.doc20.html#3273">(&#167;20.22.1)</a>, then <code>null</code> is returned.<p>
<a name="3276"></a>
<p><font size=+1><strong>20.22.4   </strong> <code>public String <code><b>toString</b></code>()</code></font>
<p>
<a name="27796"></a>
If this <code>Throwable</code> object was created with an error message string <a href="javalang.doc20.html#3274">(&#167;20.22.2)</a>, 
then the result is the concatenation of three strings:
<p><ul><a name="27809"></a>
<li>The name of the actual class of this object
<a name="27817"></a>
<li><code>": "</code> (a colon and a space)
<a name="27814"></a>
<li>The result of the <code>getMessage</code> method <a href="javalang.doc20.html#3275">(&#167;20.22.3)</a> for this object
</ul><a name="27800"></a>
If this <code>Throwable</code> object was created with no error message string <a href="javalang.doc20.html#3273">(&#167;20.22.1)</a>, then the name of the actual class of this object is returned.<p>
<a name="10057"></a>
<p><font size=+1><strong>20.22.5   </strong> <code>public Throwable <code><b>fillInStackTrace</b></code>()</code></font>
<p>
<a name="27837"></a>
This method records within this <code>Throwable</code> object information about the current 
state of the stack frames for the current thread.
<p><a name="10058"></a>
<p><font size=+1><strong>20.22.6   </strong> <code>public void <code><b>printStackTrace</b></code>()</code></font>
<p>
<a name="27863"></a>
This method prints a stack trace for this <code>Throwable</code> object on the error output 
stream that is the value of the field <code>System.err</code> <a href="javalang.doc17.html#14132">(&#167;20.18.3)</a>. The first line of output 
contains the result of the <code>toString</code> method <a href="javalang.doc20.html#3276">(&#167;20.22.4)</a> for this object. Remaining 
lines represent data previously recorded by the method <code>fillInStackTrace</code> 
<a href="javalang.doc20.html#10057">(&#167;20.22.5)</a>. The format of this information depends on the implementation, but the 
following example may be regarded as typical:
<p><pre><a name="27895"></a>
java.lang.NullPointerException
<a name="27896"></a>	at MyClass.mash(MyClass.java:9)
<a name="27897"></a>	at MyClass.crunch(MyClass.java:6)
<a name="27898"></a>	at MyClass.main(MyClass.java:3)
</pre><a name="27893"></a>
This example was produced by running the program:
<p><pre><a name="27925"></a>
class MyClass {
<a name="27944"></a>
	public static void main(String[] argv) {
<a name="27927"></a>		crunch(null);
<a name="27928"></a>	}
<br><a name="27929"></a>	static void crunch(int[] a) {
<a name="27930"></a>		mash(a);
<a name="27931"></a>	}
<br><a name="27932"></a>
	static void mash(int[] b) {
<a name="27949"></a>		System.out.println(b[0]);
<a name="43230"></a>	}
<br></pre>

<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javalang.doc19.html">Prev</a> | <a href="javalang.doc21.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright &#169 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -