文档章节

Handling Exceptions

yinjq
 yinjq
发布于 2015/05/19 19:41
字数 445
阅读 45
收藏 0

What Is an Exception?

The term exception is shorthand for the phrase "exceptional event."

Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.


Searching the call stack for the exception handler.

The Three Kinds of Exceptions

  • The first kind of exception is the checked exception. These are exceptional conditions that a well-written application should anticipate and recover from. 

Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by ErrorRuntimeException, and their subclasses.

  • The second kind of exception is the error. These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. 

Errors are not subject to the Catch or Specify Requirement. Errors are those exceptions indicated by Error and its subclasses.

  • The third kind of exception is the runtime exception. These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. 

Runtime exceptions are not subject to the Catch or Specify Requirement. Runtime exceptions are those indicated by RuntimeException and its subclasses.

Errors and runtime exceptions are collectively known as unchecked exceptions.

The Throwable class

The Catch or Specify Requirement

Valid Java programming language code must honor the Catch or Specify Requirement. This means that code that might throw certain exceptions must be enclosed by either of the following:

Code that fails to honor the Catch or Specify Requirement will not compile.

Bypassing Catch or Specify

Some programmers consider the Catch or Specify Requirement a serious flaw in the exception mechanism and bypass it by using unchecked exceptions in place of checked exceptions. In general, this is not recommended. The section Unchecked Exceptions — The Controversy talks about when it is appropriate to use unchecked exceptions.

Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.

Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.


参考:Exceptions


© 著作权归作者所有

yinjq
粉丝 10
博文 12
码字总数 4806
作品 0
海淀
程序员
私信 提问
Java How to Program学习笔记_章节小结——第十一章_深入了解异常处理(Exception Handling: A Deeper Look)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/86034071 这章的内容比较多,有不少新概念需要好好消化一下。...

预见未来to50
01/07
0
0
Continued code execution after (throwing) an Exception

Download ExceptionHandling.zip - 16.4 KB This solution is supposed to be a starting point. Since its implementation is simple, it can easily be altered to suit your needs. Intro......

Jasper Lammers
2017/12/21
0
0
IOUtils.closeQuietly:在finally中关闭流时不需要再catch一遍IOException

在使用 stream 的时,往往要 try catch IOException。eric教导我要把流的关闭放到 finally 中去写,并且在 close 之前要判断一下是否为 null。但是 stream.close() 也会 throw IOException,...

花漾年华
04/13
18
0
Flask 0.11 发布, Python 开发的 Web 框架

Flask 0.11 发布了,Flask 是一个微型的 Python 开发的 Web 框架,示例代码: 改进记录如下: Added support to serializing top-level arrays to . This introduces a security risk in an......

messense
2016/05/29
2.5K
8
Java How to Program习题_第十一章_深入了解异常处理(Exception Handling: A Deeper Look)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/86035180 这一章的习题感觉比较抽象,感觉可以暂时跳过。等到有实际需要时再来深...

预见未来to50
01/07
0
0

没有更多内容

加载失败,请刷新页面

加载更多

一起来学Java8(四)——复合Lambda

在一起来学Java8(二)——Lambda表达式中我们学习了Lambda表达式的基本用法,现在来了解下复合Lambda。 Lambda表达式的的书写离不开函数式接口,复合Lambda的意思是在使用Lambda表达式实现函...

猿敲月下码
10分钟前
2
0
debian10使用putty配置交换机console口

前言:Linux的推广普及,需要配合解决实际应用方能有成效! 最近强迫自己用linux进行实际工作,过程很痛苦,还好通过网络一一解决,感谢各位无私网友博客的帮助! 系统:debian10 桌面:xfc...

W_Lu
41分钟前
10
0
aelf Enterprise 0.8.0 beta有奖公测,“Bug奖金计划”重磅开启

2019年9月30日,aelf Enterprise 0.8.0 beta版正式发布。aelf Enterprise 0.8.0 beta是一个完备的区块链系统, 包含完备的区块链系统、开发套件、开发文档、以及配套的基础应用和基础服务。 ...

AELF开发者社区
43分钟前
8
0
oracle 初始化数据库脚本

create user lpf identified by 123456; create tablespace lpf_ts_cms datafile '/opt/app/oracle/product/11.2.0/lpf.dbf' size 200M; alter user lpf default tablespace lpf_ts_cms; sel......

internetafei
47分钟前
7
0
深入了解Redis底层数据结构

说明 说到Redis的数据结构,我们大概会很快想到Redis的5种常见数据结构:字符串(String)、列表(List)、散列(Hash)、集合(Set)、有序集合(Sorted Set),以及他们的特点和运用场景。不过它们是...

TurboSanil
48分钟前
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部