Exception
2015-11-10 12:45
302 查看
reference: Java Doc
When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
The runtime system searches the call stack for a method that contains a block of code that can handle the exception. This block of code is called an exception handler.
These are exceptional conditions that a well-written application should anticipate and recover from.
Example, java.io.FileNotFoundException
Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by
Error
These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.
Example, java.io.IOError
Runtime Exception
These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.
These usually indicate programming bugs, such as logic errors or improper use of an API.
Example, NullPointerException
Errors and runtime exceptions are collectively known as unchecked exceptions.
Catch block: Identifies a block of code, known as an exception handler, that can handle a particular type of exception.
Finally block: Identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the
The runtime system always executes the statements within the
For example, the try block of the writeList method that you've been working with here opens a PrintWriter. The program should close that stream before exiting the writeList method. This poses a somewhat complicated problem because writeList's try block can exit in one of three ways.
1. The new FileWriter statement fails and throws an IOException.
2. The list.get(i) statement fails and throws an IndexOutOfBoundsException.
3. Everything succeeds and the try block exits normally.
2. Propagating Errors Up the Call Stack
3. Grouping and Differentiating Error Types
What is Exception
Exception is short for "exceptional event".When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
The runtime system searches the call stack for a method that contains a block of code that can handle the exception. This block of code is called an exception handler.
Three Kinds of Exceptions
Checked ExceptionThese are exceptional conditions that a well-written application should anticipate and recover from.
Example, java.io.FileNotFoundException
Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by
Error,
RuntimeException, and their subclasses.
Error
These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.
Example, java.io.IOError
Runtime Exception
These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.
These usually indicate programming bugs, such as logic errors or improper use of an API.
Example, NullPointerException
Errors and runtime exceptions are collectively known as unchecked exceptions.
Try-Catch-Finally Block
Try block: Identifies a block of code in which an exception can occur.Catch block: Identifies a block of code, known as an exception handler, that can handle a particular type of exception.
Finally block: Identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the
tryblock.
More about Finally Block
Thefinallyblock always executes when the
tryblock exits. This ensures that the
finallyblock is executed even if an unexpected exception occurs.
The runtime system always executes the statements within the
finallyblock regardless of what happens within the
tryblock. So it's the perfect place to perform cleanup.
For example, the try block of the writeList method that you've been working with here opens a PrintWriter. The program should close that stream before exiting the writeList method. This poses a somewhat complicated problem because writeList's try block can exit in one of three ways.
1. The new FileWriter statement fails and throws an IOException.
2. The list.get(i) statement fails and throws an IndexOutOfBoundsException.
3. Everything succeeds and the try block exits normally.
Advantages of Exceptions
1. Separating Error-Handling Code from "Regular" Code2. Propagating Errors Up the Call Stack
3. Grouping and Differentiating Error Types
相关文章推荐
- LightOJ 1027 - A Dangerous Maze(数论)
- Hadoop辅助排序样例二
- oc单例模式
- 喜马拉雅FM克扣主播广告分成 承诺全成逗你玩
- opencv 学习笔记1
- viewpager 实现画廊效果,带动画!
- 微信开发的access_token的获取
- 把钱投资自己的头脑上(一)
- iOS ---cell设置头像
- top 内存耗用:VSS/RSS/PSS/USS
- RGB和HSB的转换推算
- HDU5489 LIS变形
- 数组之冒泡排序法--从小到大
- 【学神】1-2Linux基本文件管理
- UIScrollView滚动视图属性及方法介绍
- long 与byte类型间相互转换
- 第十五天
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(四)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(四)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(四)