throwable.html

来自「j2se5-api-zh,java文档的中文版本」· HTML 代码 · 共 742 行 · 第 1/3 页

HTML
742
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc (build 1.5.0_03) on Wed Nov 02 09:50:08 CST 2005 --><META http-equiv="Content-Type" content="text/html; charset=gb2312"><TITLE>Throwable (Java 2 Platform SE 5.0)</TITLE><META NAME="keywords" CONTENT="java.lang.Throwable class"><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"><SCRIPT type="text/javascript">function windowTitle(){    parent.document.title="Throwable (Java 2 Platform SE 5.0)";}</SCRIPT><NOSCRIPT></NOSCRIPT></HEAD><BODY BGCOLOR="white" onload="windowTitle();"><!-- ========= START OF TOP NAVBAR ======= --><A NAME="navbar_top"><!-- --></A><A HREF="#skip-navbar_top" title="跳过导航链接"></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY=""><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">  <TR ALIGN="center" VALIGN="top">  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>概述</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>软件包</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>类</B></FONT>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/Throwable.html"><FONT CLASS="NavBarFont1"><B>使用</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>树</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>已过时</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>索引</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>帮助</B></FONT></A>&nbsp;</TD>  </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup>&nbsp;2&nbsp;Platform<br>Standard&nbsp;Ed. 5.0</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">&nbsp;<A HREF="ThreadLocal.html" title="java.lang 中的类"><B>上一个类</B></A>&nbsp;&nbsp;<A HREF="TypeNotPresentException.html" title="java.lang 中的类"><B>下一个类</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">  <A HREF="../../index.html@java_2Flang_2FThrowable.html" target="_top"><B>框架</B></A>  &nbsp;&nbsp;<A HREF="Throwable.html" target="_top"><B>无框架</B></A>  &nbsp;&nbsp;<SCRIPT type="text/javascript">  <!--  if(window==top) {    document.writeln('<A HREF="../../allclasses-noframe.html"><B>所有类</B></A>');  }  //--></SCRIPT><NOSCRIPT>  <A HREF="../../allclasses-noframe.html"><B>所有类</B></A></NOSCRIPT></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">  摘要:&nbsp;嵌套&nbsp;|&nbsp;字段&nbsp;|&nbsp;<A HREF="#constructor_summary">构造方法</A>&nbsp;|&nbsp;<A HREF="#method_summary">方法</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">详细信息:&nbsp;字段&nbsp;|&nbsp;<A HREF="#constructor_detail">构造方法</A>&nbsp;|&nbsp;<A HREF="#method_detail">方法</A></FONT></TD></TR></TABLE><A NAME="skip-navbar_top"></A><!-- ========= END OF TOP NAVBAR ========= --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.lang</FONT><BR>类 Throwable</H2><PRE><A HREF="Object.html" title="java.lang 中的类">java.lang.Object</A>  <IMG SRC="../../resources/inherit.gif" ALT="继承者 "><B>java.lang.Throwable</B></PRE><DL><DT><B>所有已实现的接口:</B> <DD>java.io.Serializable</DD></DL><DL><DT><B>直接已知子类:</B> <DD><A HREF="Error.html" title="java.lang 中的类">Error</A>, <A HREF="Exception.html" title="java.lang 中的类">Exception</A></DD></DL><HR><DL><DT><PRE>public class <B>Throwable</B><DT>extends <A HREF="Object.html" title="java.lang 中的类">Object</A><DT>implements java.io.Serializable</DL></PRE><P><code>Throwable</code> 类是 Java 语言中所有错误或异常的超类。只有当对象是此类(或其子类之一)的实例时,才能通过 Java 虚拟机或者 Java <code>throw</code> 语句抛出。类似地,只有此类或其子类之一才可以是 <code>catch</code> 子句中的参数类型。<p>两个子类的实例,<A HREF="Error.html" title="java.lang 中的类"><CODE>Error</CODE></A> 和 <A HREF="Exception.html" title="java.lang 中的类"><CODE>Exception</CODE></A>,通常用于指示发生了异常情况。通常,这些实例是在异常情况的上下文中新近创建的,因此包含了相关的信息(比如堆栈跟踪数据)。 <p>Throwable 包含了其线程创建时线程执行堆栈的快照。它还包含了给出有关错误更多信息的消息字符串。最后,它还可以包含 <i>cause(原因)</i>:另一个导致此 throwable 抛出的 throwable。此 cause 设施在 1.4 版本中首次出现。它也称为<i>异常链</i> 设施,因为 cause 自身也会有 cause,依此类推,就形成了异常链,每个异常都是由另一个异常引起的。<p>导致 throwable 可能具有 cause 的一个原因是,抛出它的类构建在低层抽象的基础上,从而高层操作由于低层操作的失败而失败。因此让低层抛出的 throwable 向外传播并非一种好的设计方法,因为它通常与高层提供的抽象不相关。此外,这样做会将高层 API 与其实现细节关联起来,并认为低层异常是经过检查的异常。抛出&ldquo;经过包装的异常&rdquo;(即包含 cause 的异常)允许高层与其调用方交流失败详细信息,而不会招致上述任何一个缺点。这种方式保留了改变高层实现而不改变其 API 的灵活性(尤其是,异常集合通过其方法抛出)。<p>导致 throwable 可能具有 cause 的另一个原因是,抛出它的方法必须符合不允许方法直接抛出 cause 的通用接口。例如,假定持久集合符合 <A HREF="../util/Collection.html" title="java.util 中的接口"><CODE>Collection</CODE></A> 接口,而其持久性在 <tt>java.io</tt> 的基础上实现。假定 <tt>put</tt> 方法的内部可以抛出 <CODE>IOException</CODE>。实现可以与其调用方交流 <tt>IOException</tt> 的详细消息,同时通过以一种合适的未检查的异常来包装 <tt>IOException</tt>,使其符合 <tt>Collection</tt> 接口。(持久集合的规范应该指示它能够抛出这种异常。)<p>Cause 可以通过两种方式与 throwable 关联起来:通过一个将 cause 看作参数的构造方法;或者通过 <A HREF="Throwable.html#initCause(java.lang.Throwable)"><CODE>initCause(Throwable)</CODE></A> 方法。对于那些希望将 cause 与其关联起来的新 throwable 类,应该提供带有 cause 的构造方法,并委托(可能间接)给一个带有 cause 的 <tt>Throwable</tt> 构造方法。例如: <pre>     try {         lowLevelOp();     } catch (LowLevelException le) {         throw new HighLevelException(le);  // Chaining-aware constructor     } </pre>因为 <tt>initCause</tt> 方法是公共的,它允许 cause 与任何 throwable 相关联,甚至包括&ldquo;遗留 throwable&rdquo;,它的实现提前将异常链机制的附件应用到 <tt>Throwable</tt>。例如: <pre>     try {         lowLevelOp();     } catch (LowLevelException le) {         throw (HighLevelException)                 new HighLevelException().initCause(le);  // Legacy constructor     } </pre><p>在版本 1.4 之前,许多 throwable 有自己的非标准异常链机制(<A HREF="ExceptionInInitializerError.html" title="java.lang 中的类"><CODE>ExceptionInInitializerError</CODE></A>、<A HREF="ClassNotFoundException.html" title="java.lang 中的类"><CODE>ClassNotFoundException</CODE></A>、<A HREF="reflect/UndeclaredThrowableException.html" title="java.lang.reflect 中的类"><CODE>UndeclaredThrowableException</CODE></A>、<A HREF="reflect/InvocationTargetException.html" title="java.lang.reflect 中的类"><CODE>InvocationTargetException</CODE></A>、<CODE>WriteAbortedException</CODE>、<CODE>PrivilegedActionException</CODE>、<CODE>PrinterIOException</CODE>、<CODE>RemoteException</CODE> 和 <CODE>javax.naming.NamingException</CODE>)。所有这些 throwable 都已经更新过,可以使用标准异常链机制,同时继续实现其&ldquo;遗留&rdquo;链机制,以保持兼容性。<p>此外,从版本 1.4 开始,许多通用的 <tt>Throwable</tt> 类(例如,<A HREF="Exception.html" title="java.lang 中的类"><CODE>Exception</CODE></A>、<A HREF="RuntimeException.html" title="java.lang 中的类"><CODE>RuntimeException</CODE></A>、<A HREF="Error.html" title="java.lang 中的类"><CODE>Error</CODE></A>)都已经更新,具有带 cause 的构造方法。由于有 <tt>initCause</tt> 方法存在,这并非严格的要求,但委托给一个带有 cause 的构造方法将更方便也更形象。 <p>根据惯例,<code>Throwable</code> 类及其子类有两个构造方法,一个不带参数,另一个带有 <code>String</code> 参数,此参数可用于生成详细消息。此外,对于那些与其相关联的 cause 的子类,它们应有两个构造方法,一个带 <code>Throwable</code>(cause),一个带 <code>String</code>(详细消息)和 <code>Throwable</code>(cause)。<p>在版本 1.4 中还引入了 <A HREF="Throwable.html#getStackTrace()"><CODE>getStackTrace()</CODE></A> 方法,它允许通过各种形式的 <A HREF="Throwable.html#printStackTrace()"><CODE>printStackTrace()</CODE></A> 方法编程访问堆栈跟踪信息,这些信息以前只能以文本形式使用。此信息已经添加到该类的<i>序列化表示形式</i>,因此 <tt>getStackTrace</tt> 和 <tt>printStackTrace</tt> 将可在反序列化时获得的 throwable 上正确操作。<P><P><DL><DT><B>从以下版本开始:</B></DT>  <DD>JDK1.0</DD><DT><B>另请参见:</B><DD><A HREF="../../serialized-form.html#java.lang.Throwable">序列化表格</A></DL><HR><P><!-- ======== CONSTRUCTOR SUMMARY ======== --><A NAME="constructor_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2"><B>构造方法摘要</B></FONT></TH></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="Throwable.html#Throwable()">Throwable</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;构造一个将 <code>null</code> 作为其详细消息的新 throwable。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="Throwable.html#Throwable(java.lang.String)">Throwable</A></B>(<A HREF="String.html" title="java.lang 中的类">String</A>&nbsp;message)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;构造带指定详细消息的新 throwable。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="Throwable.html#Throwable(java.lang.String, java.lang.Throwable)">Throwable</A></B>(<A HREF="String.html" title="java.lang 中的类">String</A>&nbsp;message,          <A HREF="Throwable.html" title="java.lang 中的类">Throwable</A>&nbsp;cause)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;构造一个带指定详细消息和 cause 的新 throwable。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="Throwable.html#Throwable(java.lang.Throwable)">Throwable</A></B>(<A HREF="Throwable.html" title="java.lang 中的类">Throwable</A>&nbsp;cause)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;构造一个带指定 cause 和 <tt>(cause==null &#063; null :cause.toString())</tt>(它通常包含类和 <tt>cause</tt> 的详细消息)的详细消息的新 throwable。</TD></TR></TABLE>&nbsp;<!-- ========== METHOD SUMMARY =========== --><A NAME="method_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2"><B>方法摘要</B></FONT></TH></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;<A HREF="Throwable.html" title="java.lang 中的类">Throwable</A></CODE></FONT></TD><TD><CODE><B><A HREF="Throwable.html#fillInStackTrace()">fillInStackTrace</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;记录异常堆栈跟踪。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;<A HREF="Throwable.html" title="java.lang 中的类">Throwable</A></CODE></FONT></TD><TD><CODE><B><A HREF="Throwable.html#getCause()">getCause</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返回此 throwable 的 cause;或者如果 cause 不存在或未知,则返回 <code>null</code>。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;<A HREF="String.html" title="java.lang 中的类">String</A></CODE></FONT></TD><TD><CODE><B><A HREF="Throwable.html#getLocalizedMessage()">getLocalizedMessage</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;创建此 throwable 的本地化描述。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;<A HREF="String.html" title="java.lang 中的类">String</A></CODE></FONT></TD><TD><CODE><B><A HREF="Throwable.html#getMessage()">getMessage</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返回此 throwable 的详细消息字符串。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;<A HREF="StackTraceElement.html" title="java.lang 中的类">StackTraceElement</A>[]</CODE></FONT></TD><TD><CODE><B><A HREF="Throwable.html#getStackTrace()">getStackTrace</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;提供编程访问由 <A HREF="Throwable.html#printStackTrace()"><CODE>printStackTrace()</CODE></A> 输出的堆栈跟踪信息。</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;<A HREF="Throwable.html" title="java.lang 中的类">Throwable</A></CODE></FONT></TD><TD><CODE><B><A HREF="Throwable.html#initCause(java.lang.Throwable)">initCause</A></B>(<A HREF="Throwable.html" title="java.lang 中的类">Throwable</A>&nbsp;cause)</CODE>

⌨️ 快捷键说明

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