Java程序设计
Java Programming
Spring, 2013
课件Java程序设计方案
1
Contents
Why Exceptions?
What are Exceptions?
Handling Exceptions
Creating Exception Types
课件Java程序设计方案
2
Why Exceptions(异常)?
During execution(执行), programs can run into many kinds of errors;
What do we do when an error occurs?
Java uses exceptions to provide the error-handling capabilities for its programs.
课件Java程序设计方案
3
Exceptions (异常)
Error Class
Critical(严重的) error which is not acceptable in normal application program.
Exception Class
Possible exception in normal application program execution;
Possible to handle by programmer.
课件Java程序设计方案
4
Exceptions (异常)
An exception is an event(事件) that occurs during the execution of a program that disrupts(使中断) the normal flow(流程) of instructions(指令).
课件Java程序设计方案
5
Exceptions (异常)
Treat exception as an object .
All exceptions are instances of a class extended from Throwable class or its subclass.
Generally, a programmer makes new exception class to extend the Exception class which is a subclass of Throwable class.
课件Java程序设计方案
6
Exception Class 继承关系
Throwable
Error
Exception
RuntimeException
IOException
Object
课件Java程序设计方案
7
异常类的层次结构:
课件Java程序设计方案
8
Classifying(分类) Java Exceptions
Unchecked Exceptions(非检查性异常)
It is not required that these types of exceptions be caught or declared on a method.
Runtime exceptions can be generated by methods or by the JVM itself.
Errors are generated from deep within the JVM, and often indicate a truly fatal state.
Checked Exceptions(检查性异常)
Must either be caught by a method or declared in its signature by placing exceptions in the method signature.
课件Java程序设计方案
9
Exception的分类
非检查性异常(unchecked exception):
以RuntimeException为代表的一些类,编译时发现不了,只在能运行时才能发现。
检查性异常(checked exception):
一般程序中可预知的问题,其产生的异常可能会带来意想不到的结果,因此Java编译器要求Java程序必须捕获或声明所有的非运行时异常。
以IOException为代表的一些类。如果代码中存在检查性异常,必须进行异常处理,否则编译不能通过。如:用户连接数据库SQLException、。
课件Java程序设计方案
10
课件Java程序设计方案 来自淘豆网m.daumloan.com转载请标明出处.