课件java程序设计方案

上传人:xiao****1972 文档编号:74180075 上传时间:2019-01-27 格式:PPT 页数:47 大小:576.81KB
返回 下载 相关 举报
课件java程序设计方案_第1页
第1页 / 共47页
课件java程序设计方案_第2页
第2页 / 共47页
课件java程序设计方案_第3页
第3页 / 共47页
课件java程序设计方案_第4页
第4页 / 共47页
课件java程序设计方案_第5页
第5页 / 共47页
点击查看更多>>
资源描述

《课件java程序设计方案》由会员分享,可在线阅读,更多相关《课件java程序设计方案(47页珍藏版)》请在金锄头文库上搜索。

1、Java程序设计,Java Programming Spring, 2013,chapter 8 Exceptions,2/47,Contents,Why Exceptions? What are Exceptions? Handling Exceptions Creating Exception Types,chapter 8 Exceptions,3/47,Why Exceptions(异常)?,During execution(执行), programs can run into many kinds of errors; What do we do when an error occu

2、rs? Java uses exceptions to provide the error-handling capabilities for its programs.,chapter 8 Exceptions,4/47,Exceptions (异常),Error Class Critical(严重的) error which is not acceptable in normal application program. Exception Class Possible exception in normal application program execution; Possible

3、to handle by programmer.,chapter 8 Exceptions,5/47,Exceptions (异常),An exception is an event(事件) that occurs during the execution of a program that disrupts(使中断) the normal flow(流程) of instructions(指令).,chapter 8 Exceptions,6/47,Exceptions (异常),Treat exception as an object . All exceptions are instan

4、ces 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.,7,Exception Class 继承关系,8,异常类的层次结构:,9,Classifying(分类) Java Exceptions,Unchecked Exceptions(非检查性异常) It is not required t

5、hat 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

6、signature by placing exceptions in the method signature.,chapter 8 Exceptions,10/47,Exception的分类,非检查性异常(unchecked exception): 以RuntimeException为代表的一些类,编译时发现不了,只在能运行时才能发现。 检查性异常(checked exception): 一般程序中可预知的问题,其产生的异常可能会带来意想不到的结果,因此Java编译器要求Java程序必须捕获或声明所有的非运行时异常。 以IOException为代表的一些类。如果代码中存在检查性异常,必须进行

7、异常处理,否则编译不能通过。如:用户连接数据库SQLException、FileNotFoundException。,chapter 8 Exceptions,11/47,Exception的分类,什么是Runtime Exception: Java虚拟机在运行时生成的异常,如:被0除等系统错误、数组下标超范围等,其产生比较频繁,处理麻烦,对程序可读性和运行效率影响太大。因此由系统检测, 用户可不做处理,系统将它们交给默认的异常处理程序(当然,必要时,用户可对其处理)。 Java程序异常处理的原则是: 对于Error和RuntimeException,可以在程序中进行捕获和处理,但不是必须的。

8、 对于IOException及其他异常,必须在程序进行捕获和处理。异常处理机制主要处理检查性异常。,12,Java Exception Type Hierarchy(层次),virtual machine errors,chapter 8 Exceptions,13/47,系统异常类的层次结构:,chapter 8 Exceptions,14/47,Exception的分类,System-Defined Exception(系统定义的异常) Programmer-Defined Exception(程序员自定义异常),chapter 8 Exceptions,15/47,System-Defi

9、ned Exception (系统定义的异常),Raised implicitly by system because of illegal execution of program; Created by Java System automatically; Exception extended from Error class and RuntimeException class.,16/47,System-Defined Exception,IndexOutOfBoundsException: When beyond the bound of index in the object wh

10、ich use index, such as array, string, and vector ArrayStoreException: When assign object of incorrect type to element of array NegativeArraySizeException: When using a negative size of array NullPointerException: When refer to object as a null pointer SecurityException: When violate security. Caused

11、 by security manager IllegalMonitorStateException: When the thread which is not owner of monitor involves wait or notify method,chapter 8 Exceptions,17/47,Programmer-Defined Exception (程序员自定义异常),Exceptions raised by programmer Subclass of Exception class Check by compiler whether the exception handl

12、er for exception occurred exists or not If there is no handler, it is error.,18/47,User-defined Exceptions (用户自定义异常),class UserErr extends Exception ,Creating Exception Types:,chapter 8 Exceptions,19/47,Example 1:,public class InsufficientFundsException extends Exception private BankAccount excepban

13、k; private double excepAmount; InsufficientFundsException(Bank ba, double dAmount) excepbank=ba; excepAmount=dAmount; ,20,程序运行时发生异常,系统会抛出异常,如果程序中未处理和捕获异常,异常抛出后程序运行中断。,public class Test public int bar() int a = new int2; for (int x = 0; x = 2; x+) ax = 0; /抛出异常,程序中断 return a; public static void main(

14、String args) Test t = new Test(); t.bar(); /程序中断 System.out.println(“Method: foo”); /不被执行 ,系统给出的错误信息: Exception in thread “main“ java.lang.ArrayIndexOutOfBoundsException: 2 at exception.Test.bar(Test.java:7) at exception.Test.main(Test.java:25),chapter 8 Exceptions,21/47,Handling Exceptions(处理异常),Th

15、rowing an exception(抛出异常) When an error occurs within a method. An exception object is created and handed off to the runtime system(运行时系统). The runtime system must find the code to handle the error. Catching an exception(捕获异常) The runtime system(运行时系统) searches for code to handle the thrown exceptio

16、n. It can be in the same method or in some method in the call(调用) stack(堆栈).,22/47,Keywords for Java Exceptions,try Marks the start of a block associated with a set of exception handlers. catch If the block enclosed by the try generates an exception of this type, control moves here; watch out for implicit subsumption. finally Always called when the try block concludes, and after any necessary catch handler is complete. throws Describes the ex

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号