📄 builder_course_112_1_gjbc.html
字号:
</td>
</tr>
<tr>
<td colspan="3" align="left" valign="top">
<hr>正 文:<br>
<font class="a"><center><b>面 向 对 象 的 例 外 处 理</b></center><p align="right">作 者 : 清 华 大 学 教 授 : 王 克 宏<p><p>
什 么 是 例 外 、 例 外 处 理<p>
例 外 就 是 在 正 常 情 况 以 外 的 事 件 。 Java 之 中 的 例 外 是 指 程 序 运 行 中 发 生 的 异 常 事 件 , 这 些 异 常 事 件 将 阻 止 程 序 的 正 常 运 行 。 比 如 出 现 了 数 组 越 界 、 用 户 输 入 错 误 等 等 。 而 顾 名 思 义 , 例 外 处 理 就 是 对 这 些 例 外 进 行 处 理 。
<p>
所 有 的 高 级 计 算 机 编 程 语 言 里 都 有 判 断 是 否 出 现 了 例 外 的 语 句 , 每 个 好 的 计 算 机 程 序 都 会 有 对 例 外 进 行 处 理 的 程 序 段 。 只 不 过 在 不 同 的 计 算 机 编 程 语 言 里 面 , 对 例 外 的 称 呼 不 同 , 例 如 有 的 语 言 里 所 讲 的 错 误 处 理 , 其 实 就 等 同 于 Java 里 面 的 例 外 处 理 。 这 就 像 诸 葛 亮 先 生 有 两 个 名 字 , 孔 明 是 他 诸 葛 亮 也 是 他 , 还 有 人 称 呼 他 为 诸 葛 先 生 … … , 只 是 称 呼 不 同 罢 了 , 意 义 是 一 样 的 。
<p>
抛 弃 之<p>
在 许 多 高 级 语 言 中 都 是 通 过 使 用 if 语 句 来 判 断 是 否 出 现 了 例 外 , 并 进 行 处 理 的 。 Java 作 为 一 个 完 全 基 于 面 向 对 象 的 语 言 , 例 外 处 理 也 是 采 用 面 向 对 象 的 方 法 。 在 一 个 方 法 的 运 行 过 程 中 如 果 发 生 了 例 外 , 则 这 个 方 法 将 生 成 一 个 代 表 该 例 外 的 对 象 , 并 把 它 提 交 给 正 在 运 行 这 个 方 法 的 系 统 。 我 们 把 这 个 过 程 称 为 抛 弃 一 个 例 外 。<p>
就 像 抛 弃 这 个 球 一 样 , 把 它 给 抛 掉 了 。
抛 弃 一 个 例 外 的 过 程 就 和 我 们 平 时 工 作 中 碰 到 了 特 殊 情 况 无 法 解 决 , 需 要 写 一 份 报 告 交 把 这 个 棘 手 的 问 题 上 报 上 级 主 管 部 门 一 样 。
<p>
捕 获 之
<p> 系 统 在 运 行 的 时 候 查 找 处 理 提 交 给 它 的 例 外 的 方 法 , 这 个 过 程 称 为 捕 获 一 个 例 外 。 这 就 和 法 官 判 案 时 查 找 相 应 的 法 律 条 文 的 过 程 是 一 样 的 。
<p>
优 越 性<p>
1. 利 用 以 上 这 些 方 法 处 理 例 外 , 使 得 例 外 处 理 的 程 序 代 码 与 普 通 的 程 序 代 码 不 再 混 为 一 体 , 减 少 了 编 程 序 的 工 作 量 , 同 时 也 增 加 了 程 序 可 读 性 。<p>
2. 利 用 面 向 对 象 的 方 法 来 处 理 例 外 , 可 以 对 各 种 不 同 的 例 外 事 件 进 行 分 类 , 然 后 再 处 理 之 , 从 而 具 有 了 良 好 的 接 口 和 层 次 性 。<p>
3. 利 用 类 的 层 次 性 既 可 以 区 分 不 同 的 例 外 分 别 处 理 , 也 可 以 把 具 有 相 同 父 类 的 多 个 例 外 统 一 处 理 , 具 有 相 当 的 灵 活 性 。
<p> 由 以 上 几 点 可 知 , Java 的 这 种 面 向 对 象 的 例 外 处 理 机 制 为 那 些 具 有 动 态 运 行 特 性 的 复 杂 程 序 提 供 了 强 有 力 的 控 制 方 式 。
<p>
以 上 这 些 优 越 性 背 下 来 就 可 以 了 , 到 时 候 可 以 用 来 在 孩 儿 们 面 前 炫 耀 一 下 自 己 的 知 识 渊 博 。
<p>
与 例 外 处 理 相 关 的 类
<p> 用 面 向 对 象 的 方 法 来 进 行 例 外 处 理 首 先 必 须 建 立 类 的 层 次 。 如 图 20.1 所 示 , 类 Throwable 是 最 顶 层 的 , 只 有 它 的 子 类 才 可 以 作 为 一 个 例 外 被 抛 弃 。
<p><p>
<center>
第1页
<a href="builder_course_112_2_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_112_2_GJBC.html">第2页</a>
<a href="builder_course_112_3_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_112_3_GJBC.html">第3页</a>
<a href="builder_course_112_4_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_112_4_GJBC.html">第4页</a>
<a href="builder_course_112_2_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_112_2_GJBC.html">下一页</a>
</center>
<p><hr>
</td>
</tr>
<tr>
<td width="400" align="left" valign="top"><br>
<b>参 考 文 章 : </b><br>
<font class="a">1. <a href="builder_course_111_1_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_111_1_GJBC.html">Java应 用 教 程 (七 )—— Java的 网 络 功 能</a> ( 1998-08-03 )</font><br>
<font class="a">2. <a href="builder_course_110_1_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_110_1_GJBC.html">Java应 用 教 程 (六 )—— 终 于 动 起 来 了</a> ( 1998-07-03 )</font><br>
<font class="a">3. <a href="builder_course_109_1_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_109_1_GJBC.html">Java应 用 教 程 (五 )—— 交 互 的 Java世 界</a> ( 1998-05-08 )</font><br>
<font class="a">4. <a href="builder_course_108_1_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_108_1_GJBC.html">Java应 用 教 程 (四 )—— 五 彩 缤 纷 的 世 界</a> ( 1998-03-11 )</font><br>
<font class="a">5. <a href="builder_course_105_1_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_105_1_GJBC.html">Java应 用 教 程 (三 )—— 漂 亮 的 标 语</a> ( 1998-01-27 )</font><br>
<font class="a">6. <a href="builder_course_104_1_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_104_1_GJBC.html">Java应 用 教 程 (二 )—— 巧 手 木 匠</a> ( 1997-12-16 )</font><br>
<font class="a">7. <a href="builder_course_100_1_GJBC.html" tppabs="http://www.chinabyte.com/staticpages/builder/builder_course/GJBC/builder_course_100_1_GJBC.html">Java应 用 教 程 (一)——美 丽 的 窗 口</a> ( 1997-11-04 )</font><br>
</td>
<td width="20"><img src="../main/images/blank.gif" tppabs="http://www.chinabyte.com/images/blank.gif" border="0" width="20" height="1">
</td>
<td width="204" align="left" valign="top"><br>
<font class="a">
如 果 您 认 为 这 篇 文 章 还 不 错 , 欢 迎 您 向 其 他 读 者 推 荐 ; 如 果 您 认 为 这 篇 文 章 水 平 太 差 , 欢 迎 您 <a href="mailto:buildermaster@chinabyte.com">发 表 高 见</a> 。</font>
</td>
</tr>
</table>
<p>
<table border="0" cellspacing="0" cellpadding="0" bgcolor="#980018">
<tr>
<td bgcolor="ffffff"><img src="../main/images/b_left.gif" tppabs="http://www.chinabyte.com/images/b_left.gif" border="0" WIDTH="19" HEIGHT="27"></td>
<td>
<a href="../tppmsgs/msgs2.htm#250" tppabs="http://www.chinabyte.com/staticpages/feedback/feedback_home/feedback/feedback_home_1.html" onMouseOver="window.status = '服务台'; return true "><img src="../main/images/99feedback.gif" tppabs="http://www.chinabyte.com/images/99feedback.gif" border="0" alt="服务台" WIDTH="39" HEIGHT="27"></a><img src="../main/images/b_1.gif" tppabs="http://www.chinabyte.com/images/b_1.gif" WIDTH="10" HEIGHT="27"><img src="../main/images/b_2.gif" tppabs="http://www.chinabyte.com/images/b_2.gif" WIDTH="11" HEIGHT="27"></td>
<td>
<a href="../tppmsgs/msgs2.htm#251" tppabs="http://www.chinabyte.com/staticpages/siteinfo/siteinfo_mapind/siteinfo/siteinfo_mapind_1.html" onMouseOver="window.status = '站导图'; return true "><img src="../main/images/99site.gif" tppabs="http://www.chinabyte.com/images/99site.gif" border="0" alt="站导图" WIDTH="38" HEIGHT="27"></a><img src="../main/images/b_1.gif" tppabs="http://www.chinabyte.com/images/b_1.gif" WIDTH="10" HEIGHT="27"><img src="../main/images/b_2.gif" tppabs="http://www.chinabyte.com/images/b_2.gif" WIDTH="11" HEIGHT="27"></td>
<td>
<a href="../tppmsgs/msgs2.htm#252" tppabs="http://www.chinabyte.com/feedback/about_chinabyte.html" onMouseOver="window.status = '关于本站'; return true "><img src="../main/images/99about.gif" tppabs="http://www.chinabyte.com/images/99about.gif" border="0" alt="关于本站" WIDTH="52" HEIGHT="27"></a><img src="../main/images/b_1.gif" tppabs="http://www.chinabyte.com/images/b_1.gif" WIDTH="10" HEIGHT="27"><img src="../main/images/b_2.gif" tppabs="http://www.chinabyte.com/images/b_2.gif" WIDTH="11" HEIGHT="27"></td>
<td width="131" bgcolor="#980018"><img src="../main/images/blank.gif" tppabs="http://www.chinabyte.com/images/blank.gif" border="0" width="1" height="1"></td>
<td><a HREF="../tppmsgs/msgs2.htm#200" tppabs="http://www.chinabyte.com/staticpages/digest/digest_home/digest/digest_home_1.html" onMouseOver="window.status = '及时传送IT产业新闻'; return true" target="_top"><img src="../main/images/99news.gif" tppabs="http://www.chinabyte.com/images/99news.gif" border="0" alt="新闻总线" WIDTH="53" HEIGHT="27"></a><img src="../main/images/b_1.gif" tppabs="http://www.chinabyte.com/images/b_1.gif" WIDTH="10" HEIGHT="27"><img src="../main/images/b_2.gif" tppabs="http://www.chinabyte.com/images/b_2.gif" WIDTH="11" HEIGHT="27"></td>
<td><a HREF="../tppmsgs/msgs2.htm#202" tppabs="http://www.chinabyte.com/staticpages/software/software_home/software/software_home_1.html" onMouseOver="window.status = '漫步免费软件仓库'; return true" target="_top"><img src="../main/images/99soft.gif" tppabs="http://www.chinabyte.com/images/99soft.gif" border="0" alt="软件仓库" WIDTH="52" HEIGHT="27"></a><img src="../main/images/b_1.gif" tppabs="http://www.chinabyte.com/images/b_1.gif" WIDTH="10" HEIGHT="27"><img src="../main/images/b_2.gif" tppabs="http://www.chinabyte.com/images/b_2.gif" WIDTH="11" HEIGHT="27"></td>
<td><a HREF="../tppmsgs/msgs2.htm#204" tppabs="http://www.chinabyte.com/staticpages/builder/builder_home/builder/builder_home_1.html" onMouseOver="window.status = '在线专业培训'; return true" target="_top"><img src="../main/images/99_builder.gif" tppabs="http://www.chinabyte.com/images/99_builder.gif" border="0" alt="网络学院" WIDTH="52" HEIGHT="27"></a><img src="../main/images/b_1.gif" tppabs="http://www.chinabyte.com/images/b_1.gif" WIDTH="10" HEIGHT="27"><img src="../main/images/b_2.gif" tppabs="http://www.chinabyte.com/images/b_2.gif" WIDTH="11" HEIGHT="27"></td>
<td><a HREF="../tppmsgs/msgs2.htm#213" tppabs="http://www.chinabyte.com/staticpages/gamecen/gamecen_home/gamecen/gamecen_home_1.html" onMouseOver="window.status = '游戏玩家的乐园'; return true" target="_top"><img src="../main/images/99game.gif" tppabs="http://www.chinabyte.com/images/99game.gif" border="0" alt="游戏天堂" WIDTH="52" HEIGHT="27"></a><img src="../main/images/b_1.gif" tppabs="http://www.chinabyte.com/images/b_1.gif" WIDTH="10" HEIGHT="27"><img src="../main/images/b_2.gif" tppabs="http://www.chinabyte.com/images/b_2.gif" WIDTH="11" HEIGHT="27"></td>
<td><a HREF="../tppmsgs/msgs2.htm#214" tppabs="http://www.chinabyte.com/staticpages/digital/digital_home/digital/digital_home_1.html" onMouseOver="window.status = '数字世界'; return true" target="_top"><img src="../main/images/99digital.gif" tppabs="http://www.chinabyte.com/images/99digital.gif" border="0" alt="数字世界" WIDTH="53" HEIGHT="27"></a><img src="../main/images/b_1.gif" tppabs="http://www.chinabyte.com/images/b_1.gif" WIDTH="10" HEIGHT="27"><img src="../main/images/b_2.gif" tppabs="http://www.chinabyte.com/images/b_2.gif" WIDTH="11" HEIGHT="27"></td>
<td width="30"><img src="../main/images/blank.gif" tppabs="http://www.chinabyte.com/images/blank.gif" border="0" width="1" height="1"></td>
</tr>
</table>
<center><table>
<tr>
<td align="center"><br><font size="-2">
<b>ChinaByte</b>版 权 所 有, 未 经 授 权 禁 止 复 制 或 建 立 镜 像。<br>
如 果 您 有 什 么 问 题, 请 发 <a href="mailto:webmaster@chinabyte.com">电 子 邮 件</a> 给 我 们。 谢 谢! </font>
</td>
</tr>
</table></center>
<!--msnavigation--></td></tr><!--msnavigation--></table></body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -