亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? chap10.htm

?? Thinking in Java, 2nd edition
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:17
Translation Platform:Win32
Number of Output files:23
This File:Chap10.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>10: Error Handling  with Exceptions</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README.txt">Viewing Hints</a> ]
    [ <a href="RevHist.htm">Revision History</a> ]
    [ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]
    [ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Seminars">Seminars</a> ]
    [ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/Services">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 2nd edition, Revision 12</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;2000 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chap09.htm">Previous Chapter</a> ] 
    [ <a href="SimpCont.htm">Short TOC</a> ] 
    [ <a href="Contents.htm">Table of Contents</a> ] 
    [ <a href="DocIdx.htm">Index</a> ]
     [ <a href="Chap11.htm">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="Chapter_9"></A><A NAME="_Toc375545361"></A><A NAME="_Toc477690730"></A><A NAME="_Toc481064713"></A><A NAME="Heading331"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
10: Error Handling <BR>with Exceptions<A NAME="OLE_LINK1"></A></H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER10_I0>
The
basic philosophy of Java is that &#8220;badly formed code will not be
run.&#8221;</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The ideal time to catch an
<A NAME="Index1049"></A><A NAME="Index1050"></A>error is at compile-time, before
you even try to run the program. However, not all errors can be detected at
compile-time. The rest of the problems must be handled at run-time, through some
formality that allows the originator of the error to pass appropriate
information to a recipient who will know how to handle the difficulty properly.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I0' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In C and other earlier languages, there
could be several of these formalities, and they were generally established by
convention and not as part of the programming language. Typically, you returned
a special value or set a flag, and the recipient was supposed to look at the
value or the flag and determine that something was amiss. However, as the years
passed, it was discovered that programmers who use a library tend to think of
themselves as invincible&#8212;as in, &#8220;Yes, errors might happen to others,
but not in <I>my</I> code.&#8221; So, not too surprisingly, they wouldn&#8217;t
check for the error conditions (and sometimes the error conditions were too
silly to check
for</FONT><A NAME="fnB51" HREF="#fn51">[51]</A><FONT FACE="Georgia">). If you
<I>were</I> thorough enough to check for an error every time you called a
method, your code could turn into an unreadable nightmare. Because programmers
could still coax systems out of these languages they were resistant to admitting
the truth: This approach to handling errors was a major limitation to creating
large, robust, maintainable programs.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I1' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I2>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The solution is to take the casual nature
out of error handling and to enforce formality. This actually has a long
history, since implementations of <I>exception handling</I> go back to operating
systems in the 1960s, and even to BASIC&#8217;s &#8220;<B>on error
goto</B>.&#8221; But C++ exception handling was based on Ada, and Java&#8217;s
is based primarily on C++ (although it looks even more like Object Pascal).

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I2' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I3>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The word &#8220;exception&#8221; is meant
in the sense of &#8220;I take exception to that.&#8221; At the point where the
problem occurs you might not know what to do with it, but you do know that you
can&#8217;t just continue on merrily; you must stop and somebody, somewhere,
must figure out what to do. But you don&#8217;t have enough information in the
current context to fix the problem. So you hand the problem out to a higher
context where someone is qualified to make the proper decision (much like a
chain of command). 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I3' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The other rather significant benefit of
exceptions is that they clean up error handling code. Instead of checking for a
particular error and dealing with it at multiple places in your program, you no
longer need to check at the point of the method call (since the exception will
guarantee that someone catches it). And, you need to handle the problem in only
one place, the so-called <A NAME="Index1051"></A><I>exception handler</I>. This
saves you code, and it separates the code that describes what you want to do
from the code that is executed when things go awry. In general, reading,
writing, and debugging code becomes much clearer with exceptions than when using
the old way of error handling.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I4' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I5>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Because exception handling is enforced by
the Java compiler, there are only so many examples that can be written in this
book without learning about exception handling. This chapter introduces you to
the code you need to write to properly handle exceptions, and the way you can
generate your own exceptions if one of your methods gets into trouble.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I5' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I6>
</FONT><A NAME="_Toc375545362"></A><A NAME="_Toc481064714"></A><BR></P></DIV>
<A NAME="Heading332"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Basic exceptions</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">An <A NAME="Index1052"></A><I>exceptional
condition</I> is a problem that prevents the continuation of the method or scope
that you&#8217;re in. It&#8217;s important to distinguish an exceptional
condition from a normal problem, in which you have enough information in the
current context to somehow cope with the difficulty. With an exceptional
condition, you cannot continue processing because you don&#8217;t have the
information necessary to deal with the problem <I>in the current context</I>.
All you can do is jump out of the current context and relegate that problem to a
higher context. This is what happens when you throw an exception.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I6' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I7>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A simple example is a divide. If
you&#8217;re about to divide by zero, it&#8217;s worth checking to make sure you
don&#8217;t go ahead and perform the divide. But what does it mean that the
denominator is zero? Maybe you know, in the context of the problem you&#8217;re
trying to solve in that particular method, how to deal with a zero denominator.
But if it&#8217;s an unexpected value, you can&#8217;t deal with it and so must
throw an exception rather than continuing along that path.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I7' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I8>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you throw an
<A NAME="Index1053"></A>exception, several things happen. First, the exception
object is created in the same way that any Java object is created: on the heap,
with <B>new</B>. Then the current path of execution (the one you couldn&#8217;t
continue) is stopped and the reference for the exception object is ejected from
the current context. At this point the exception handling mechanism takes over
and begins to look for an appropriate place to continue executing the program.
This appropriate place is the <I>exception handler,</I> whose job is to recover
from the problem so the program can either try another tack or just continue.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I8' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I9>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As a simple example of throwing an
exception, consider an object reference called <B>t</B>. It&#8217;s possible
that you might be passed a reference that hasn&#8217;t been initialized, so you
might want to check before trying to call a method using that object reference.
You can send information about the error into a larger context by creating an
object representing your information and &#8220;throwing&#8221; it out of your
current context. This is called <I>throwing an exception.</I> Here&#8217;s what
it looks like:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>if</font>(t == <font color=#0000ff>null</font>)
  <font color=#0000ff>throw</font> <font color=#0000ff>new</font> NullPointerException();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This throws the exception, which allows
you&#8212;in the current context&#8212;to abdicate responsibility for thinking
about the issue further. It&#8217;s just magically handled somewhere else.
Precisely <I>where </I>will be shown shortly.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I9' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I10>
</FONT><A NAME="_Toc375545363"></A><A NAME="_Toc481064715"></A><BR></P></DIV>
<A NAME="Heading333"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Exception arguments<BR><A NAME="Index1054"></A><A NAME="Index1055"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Like any object in Java, you always
create exceptions on the heap using <B>new</B>, which allocates storage and
calls a constructor. There are two constructors in all standard exceptions: the
first is the default constructor, and the second takes a string argument so you
can place pertinent information in the exception:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>if</font>(t == <font color=#0000ff>null</font>)
  <font color=#0000ff>throw</font> <font color=#0000ff>new</font> NullPointerException(<font color=#004488>"t = null"</font>);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This string can later be extracted using
various methods, as you&#8217;ll see.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER10_I10' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER10_I11>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <A NAME="Index1056"></A>keyword
<B>throw</B> causes a number of relatively magical things to happen. Typically,

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产一区欧美一区| 日韩和欧美一区二区| 91蜜桃婷婷狠狠久久综合9色| 亚洲午夜精品在线| 国产女人水真多18毛片18精品视频 | 日韩理论电影院| 精品国产乱子伦一区| 欧美日韩国产首页| 91老司机福利 在线| 狠狠色综合播放一区二区| 亚洲一区av在线| 中文字幕一区免费在线观看| 精品va天堂亚洲国产| 欧美喷潮久久久xxxxx| 91捆绑美女网站| 成人网男人的天堂| 国产精品中文欧美| 麻豆91在线观看| 日韩中文字幕一区二区三区| 一区二区三区日韩在线观看| 中文字幕一区二区三区乱码在线| 久久综合精品国产一区二区三区| 欧美乱熟臀69xxxxxx| 欧美丝袜丝交足nylons图片| 9i看片成人免费高清| 国产suv精品一区二区三区| 久国产精品韩国三级视频| 日本亚洲一区二区| 午夜激情一区二区| 午夜激情久久久| 日日摸夜夜添夜夜添国产精品| 亚洲黄色小视频| 亚洲影院理伦片| 亚洲一区中文日韩| 亚洲国产成人porn| 亚洲午夜久久久久久久久电影院 | 国产精品一卡二卡| 国产一区二区精品久久| 国产一区二区三区蝌蚪| 裸体健美xxxx欧美裸体表演| 日韩制服丝袜先锋影音| 日本不卡的三区四区五区| 日韩不卡一二三区| 精品中文字幕一区二区| 国产一区二区三区免费在线观看| 国产激情一区二区三区桃花岛亚洲 | 美女视频一区在线观看| 久久99深爱久久99精品| 国产一区高清在线| 成人ar影院免费观看视频| 99久久精品情趣| 色综合亚洲欧洲| 日本韩国一区二区三区视频| 欧美视频在线观看一区二区| 91精品国产综合久久香蕉的特点| 欧美一区二区三区成人| 欧美不卡一区二区三区四区| 精品国产1区2区3区| 欧美极品美女视频| 亚洲乱码国产乱码精品精的特点| 一区二区三区国产| 日本 国产 欧美色综合| 国产精品一区三区| 91在线观看一区二区| 欧美日韩一区在线观看| 欧美成人猛片aaaaaaa| 国产欧美日韩一区二区三区在线观看| 亚洲三级在线播放| 日韩中文字幕不卡| 高清在线成人网| 欧美视频一区在线| 久久精品男人的天堂| 亚洲免费在线看| 麻豆高清免费国产一区| 成人黄色电影在线| 欧美日韩高清在线| 欧美国产97人人爽人人喊| 亚洲激情五月婷婷| 国产精品中文字幕一区二区三区| 91麻豆免费视频| 日韩视频中午一区| 国产精品福利一区二区三区| 五月天国产精品| 成人国产精品免费观看动漫| 欧美日韩国产综合一区二区三区| 国产欧美一区二区精品忘忧草 | 欧美亚洲国产一区二区三区| 精品国产一二三| 亚洲国产精品久久人人爱蜜臀| 韩国精品主播一区二区在线观看| 色av综合在线| 欧美高清在线一区二区| 午夜精品福利一区二区三区av| eeuss鲁片一区二区三区 | 色欧美片视频在线观看| 亚洲精品一区二区三区四区高清| 一区二区三国产精华液| 国产91对白在线观看九色| 日韩一区二区精品葵司在线| 一区二区三区四区不卡在线 | 日韩亚洲欧美综合| 亚洲免费资源在线播放| 国产福利一区二区三区视频| 欧美一级淫片007| 亚洲综合色区另类av| av中文字幕在线不卡| 久久香蕉国产线看观看99| 偷窥国产亚洲免费视频| 色哦色哦哦色天天综合| 亚洲国产精品ⅴa在线观看| 极品少妇一区二区三区精品视频| 欧美午夜在线一二页| 亚洲激情第一区| 色婷婷亚洲一区二区三区| 国产精品久久久一区麻豆最新章节| 韩国午夜理伦三级不卡影院| 日韩一区二区三区免费观看| 午夜精品久久久| 欧美精品久久一区| 图片区小说区区亚洲影院| 欧美色图一区二区三区| 一区二区三区免费看视频| 色婷婷av一区二区三区之一色屋| 国产精品盗摄一区二区三区| 大白屁股一区二区视频| 国产视频在线观看一区二区三区| 老司机精品视频导航| 精品欧美黑人一区二区三区| 美女在线视频一区| 欧美电影免费观看高清完整版在线 | 天天色天天操综合| 欧美久久一区二区| 日韩电影在线一区| 日韩一区二区三区视频在线| 日本在线观看不卡视频| 欧美变态tickle挠乳网站| 久久99精品国产.久久久久| 精品国产乱码久久久久久闺蜜 | 奇米色777欧美一区二区| 欧美美女一区二区| 日本特黄久久久高潮| 日韩精品一区二区三区在线观看| 老司机精品视频在线| 久久久精品人体av艺术| 国产乱子轮精品视频| 中文字幕不卡在线播放| 99在线精品观看| 亚洲在线成人精品| 欧美精品久久天天躁| 久久精品99国产精品| 久久久久久综合| gogo大胆日本视频一区| 亚洲一区av在线| 日韩欧美视频一区| 国产69精品久久久久777| 一区二区三区四区在线播放| 在线播放视频一区| 国产成人av网站| 一区二区三区在线观看国产| 欧美一区二区久久久| 国产精品综合久久| 亚洲最新在线观看| 精品国产乱码久久久久久图片 | 极品瑜伽女神91| 国产精品久久久久影院老司| 在线观看不卡视频| 久久99日本精品| 亚洲卡通欧美制服中文| 91精品蜜臀在线一区尤物| 懂色av一区二区三区蜜臀| 亚洲精品大片www| 欧美第一区第二区| av电影在线观看一区| 青青草国产精品97视觉盛宴| 久久蜜桃一区二区| 欧美视频完全免费看| 国产成人在线色| 亚洲成a人片在线不卡一二三区| 欧美精品一区二区三区四区| 一本久道中文字幕精品亚洲嫩| 免费成人av在线| 亚洲欧美国产毛片在线| 日韩亚洲欧美高清| 欧美综合亚洲图片综合区| 国产高清在线精品| 午夜欧美2019年伦理| 国产精品欧美一区二区三区| 678五月天丁香亚洲综合网| eeuss鲁片一区二区三区| 理论片日本一区| 亚洲成人精品一区| 亚洲人成7777| 亚洲国产成人私人影院tom| 91精品国产品国语在线不卡| 一本久久a久久免费精品不卡| 国产成人综合在线播放| 日韩国产欧美在线观看| 亚洲欧美偷拍卡通变态| 国产亚洲一区字幕| 欧美不卡视频一区|