?? j-scjp-3-3.html
字號:
<td class="small" colspan="6"><a target="_blank" style="color: #ffffff;" href="http://202.95.2.149/help.htm">高級搜索</a><img alt="" height="1" width="1" border="0" src="../i/c.gif"></td>
</tr>
<tr>
<td colspan="6"><img alt="" height="4" width="390" border="0" src="../i/c.gif"></td>
</tr>
</form>
</table>
</td>
</tr>
<tr>
<td class="hbg" height="21" width="160"></td><td class="bbg" valign="top" height="21" colspan="2"> <a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/" class="mainlink">IBM 主頁</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/products/index.shtml" class="mainlink">產品與服務</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/support/index.shtml" class="mainlink">支持與下載</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/account/index.shtml" class="mainlink">個性化服務</a></td>
</tr>
</table>
<TABLE width="100%" cellspacing="0" cellpadding="0" border="0">
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#cc6633" height="3" width="150"><IMG src="../i/c.gif" width="150" height="3"></TD>
</TR>
<TR>
<TD bgcolor="#333333" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#000000" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG src="../i/c.gif" width="150" height="2"></TD>
</TR>
</TABLE>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><font size="-1" face="Verdana, sans-serif"><a href="http://www.ibm.com/">IBM</a> : <a href="/developerWorks/cn/index.shtml">developerWorks 中國網站</a> : <a href="/developerWorks/cn/java/index.shtml">java</a> : <a href="/developerWorks/cn/cnedu.nsf/java-onlinecourse-bytitle?OpenView&Count=500">教學 - 在線教程</a></font>
<br>
<img alt=" Java 認證成功之路,第 1 部分:SCJP " src="imagemaster/masthead.jpg"></td><td width="*" valign="bottom" align="right"><a border="0" href="j-scjp.zip"><img alt="下載 ZIP 文件" border="0" src="../i/icon-zip.gif"></a><a target="_blank" href="../tutorial_eng/index.html" border="0"><img alt="英文原文" border="0" src="../i/icon-source.gif"></a>
</td>
</tr>
<tr>
<td height="2" colspan="2"><img height="2" width="1" src="../i/c.gif"></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜單" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index3.html"><img alt="章節菜單" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scjp-11-3.html"><img alt="給出此教程的反饋意見" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-scjp-3-2.html"><img alt="上頁" border="0" src="../i/previous.gif" name="topprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scjp-3-4.html"><img alt="下頁" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="3.流程控制、斷言和異常處理 " src="imagemaster/titlebar3.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15"> </td><td bgcolor="ffffff" width="12"> </td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>異常處理 </b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr> 第 3 頁(共5 頁)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>異常是 Java 對象,異常類是從 <code>java.lang.Throwable</code>派生出來的。 </p>
<p>拋出異常用于表示出現了異常情況,如訪問超出數組大小的索引。異??梢允?checked 或者是 unchecked。 <code>RuntimeException</code> 和 <code>Error</code> 的子類稱為 <i>unchecked 異常 </i>。所有從 <code>Exception</code> 繼承的其他類都是 <i>checked 異常 </i>。所有可能拋出 checked 異常的方法都必須用 <code>throws</code> 關鍵詞(有關關鍵詞的更多內容請參閱 <a href="j-scjp-5-3.html">Java 關鍵詞和標識符 </a>) 來聲明異常,或者用 <code>try/catch</code> 塊來處理異常。 </p>
<p>
<b>
<code>try</code>/<code>catch</code> 塊 </b>
<br>
可能拋出異常的代碼被包圍在 <code>try</code> 塊中??梢蕴峁┮粋€或者多個 <code>catch</code> 子句來處理不同的異常類型: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
try
{
// code that might throw exceptions
}
catch(Exception e)
{
//code to handle the exception
}
</code>
</pre>
<p>更特定的異常類型應該首先處理。例如,我們必須從特定到最一般的順序排列 <code>catch</code> 塊,否則就會出現編譯時錯誤。 </p>
<p>例如,假設 <code>MyException</code> 繼承自 <code>Exception</code>:</p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
try
{
throw new MyException();
}
catch(Exception e)
{
System.out.println("Exception");
}
catch(MyException e)
{
System.out.println("MyException");
}
</code>
</pre>
<p>這里編譯器會報怨無法達到 <code>MyException</code> 的 <code>catch</code> 子句,因為所有拋出的異常都被第一個一般性的 <code>catch</code> 子句捕獲了。 </p>
<p>
<b>
<code>finally</code> 塊 </b>
<br>
還可以提供 <code>finally</code> 塊來執行任何清除操作。如果拋出一個異常,那么相匹配的 <code>catch</code> 子句就會執行,然后控制就會進入 <code>finally</code> 塊(如果有的話)。 <code>finally</code> 塊的語法如下: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
try
{
// code that throws exceptions
}
catch(Exception e)
{
// handle exception
}
finally
{
// cleanup code
}
</code>
</pre>
<p>
<code>finally</code> 塊即使在沒有異常拋出的情況下也會執行。惟一的例外是當 <code>try</code> 或者 <code>catch</code> 塊調用 <code>System.exit()</code> 時。一個 <code>try</code> 塊應當有一個 <code>catch</code> 塊或者有一個 <code>finally</code> 塊,但是不一定要同時有這兩者。 </p>
<p>
<b>聲明和拋出異常 </b>
<br>
要在代碼中顯式地拋出異常,可使用 <code>throw</code> 關鍵詞。 </p>
<p>方法可以拋出的 checked 異常必須用 <code>throws</code> 關鍵詞(有關關鍵詞的更多內容請參閱 <a href="j-scjp-5-3.html">Java 關鍵詞和標識符 </a>)聲明。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
void f() throws MyException
{
if(x > y) throw new MyException(); // MyException is a subclass of Exception
}
</code>
</pre>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜單" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index3.html"><img alt="章節菜單" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scjp-11-3.html"><img alt="給出此教程的反饋意見" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-scjp-3-2.html"><img alt="上頁" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scjp-3-4.html"><img alt="下頁" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">關于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隱私條約</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律條款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">聯系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
<script src="//www.ibm.com/common/stats/stats.js" language="JavaScript1.2" type="text/javascript"></script>
<noscript>
<img border="0" alt="" height="1" width="1" src="//stats.www.ibm.com/rc/images/uc.GIF?R=noscript"></noscript>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -