📄 tij0096.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0095.html">Prev</a> | <a href="tij0097.html">Next</a>
</td>
</tr></table>
<hr>
<H1 ALIGN=LEFT>
9:
Error handling
<P>with
exceptions
<a name="OLE_LINK1"></a></H1>
<DIV ALIGN=LEFT><FONT FACE="Calligraph421 BT" SIZE=4 COLOR="Black">The
basic philosophy of Java is that “badly-formed code will not be run.”
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
with C++, the ideal time to catch the <A NAME="Index902"></A><A NAME="Index903"></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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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, as in,
“Yes, errors might happen to others but not in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>my</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
code.” So, not too surprisingly, they wouldn’t check for the error
conditions (and sometimes the error conditions were too silly to check for
</FONT><A NAME="fnB41" HREF="#fn41">[41]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
If you
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>were</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>exception
handling
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
go back to operating systems in the 1960s and even to BASIC’s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>on
error goto
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
But C++ exception handling was based on Ada, and Java’s is based
primarily on C++ (although it looks even more like Object Pascal).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
word “exception” is meant in the sense of “I take exception
to that.” At the point where the problem occurs you might not know what
to do with it, but you do know that you can’t just continue on merrily;
you must stop and somebody, somewhere, must figure out what to do. But you
don’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).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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="Index904"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>exception
handler
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 the exceptions, and the way you can generate your own exceptions if one
of your methods gets into trouble.
</FONT><a name="_Toc375545362"></a><a name="_Toc408018589"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn41" HREF="#fnB41">[41]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
The C programmer can look up the return value of
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><B>printf( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
for an example of this.
</FONT><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0095.html">Prev</a> | <a href="tij0097.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -