Chapter_06 JAVA的异常处理机制

上传人:油条 文档编号:26697921 上传时间:2017-12-30 格式:PPT 页数:50 大小:734KB
返回 下载 相关 举报
Chapter_06 JAVA的异常处理机制_第1页
第1页 / 共50页
Chapter_06 JAVA的异常处理机制_第2页
第2页 / 共50页
Chapter_06 JAVA的异常处理机制_第3页
第3页 / 共50页
Chapter_06 JAVA的异常处理机制_第4页
第4页 / 共50页
Chapter_06 JAVA的异常处理机制_第5页
第5页 / 共50页
点击查看更多>>
资源描述

《Chapter_06 JAVA的异常处理机制》由会员分享,可在线阅读,更多相关《Chapter_06 JAVA的异常处理机制(50页珍藏版)》请在金锄头文库上搜索。

1、1,1,第6章 Java的异常处理机制,主要解决的问题: 1、什么是错误与异常? 2、造成Java异常的原因 3、什么是抛出?什么是捕获? 4、Java有哪些异常对象? 5、在程序中使用什么语句结构来处理异常? 6、能否自己创建异常对象?,2,2,本章主要内容,1.什么是异常处理机制2.异常类的层次结构3.异常的处理,3,3,异常示例,public class ExTestpublic static void main(String args) String friend=lisa,bily,mary;for (int i=0;i5;i+ )System.out.println(friendi

2、);System.out.println(n the end!);,4,4,public class ExTestpublic static void main(String args) String friend=lisa,bily,mary;tryfor (int i=0;i5;i+ )System.out.println(friendi);catch (IndexOutOfBoundsException e) System.out.println(数组越界,溢出!);System.out.println(n the end!);,5,5,import java.io.*;public c

3、lass EX_1public static void main(String args) throws IOExceptionString number_ch;int num;double total=3.25;System.out.println(有多少个盒子?);BufferedReader in=new BufferedReader(new InputStreamReader(System.in);number_ch=in.readLine();num=Integer.parseInt(number_ch);System.out.println(fgfg:+total*num);,6,

4、6,异常,异常:是在程序运行过程中发生的异常事件。如:除0溢出、数组越界、文件找不到等。,7,7,6.1 什么是异常处理机制,Java的异常处理机制是用来处理程序运行时的错误的有效机制,以往需要由程序员完成的程序出错情况的判别,在Java中改为由系统承担。通过系统抛出的异常,程序可以很容易地捕获并处理发生的异常情况。,8,8,6.1.1 什么是错误与异常,根据错误的性质将运行错误分为两种类型: 1Error错误:表示恢复很困难的情况下的一种严重问题。如内存溢出。不可能指望程序处理。 处理方法:不应该抛出异常。让程序中断。 2Exception异常:表示一种设计或实现问题,如对负数开平方根、空指

5、针访问等。 处理方法:编写代码来处理异常并继续程序执行,而不是让程序中断。-是程序员的责任,9,9,6.1.2 异常发生的原因,异常发生的原因有以下三种: 1Java 虚拟机检测到了非正常的执行状态 2Java程序代码中的throw语句被执行。 3异步异常发生。,10,10,6.1.3 如何处理异常,Java的异常处理机制分为两个步骤: 1抛出异常Java规定:当语义限制被违反时,将会抛出(throw)异常,即产生一个异常事件,生成一个异常对象,并把它提交给运行系统,再由运行系统寻找相应的代码来处理异常。 2捕获异常异常抛出后,运行时系统从生成异常对象的代码开始,沿方法的调用栈进行查找,直到找

6、到包含相应处理的方法代码,并把异常对象交给该方法,称为捕获(catch)异常。,11,11,抛出异常,如果一个方法中的一个语句会产生一个没有在相应的try/catch块中处理的异常,那么该方法中止执行并将异常抛出到该方法的调用程序中。这个过程要一直延续到异常被处理。如果一直到main()中仍没有被处理,那么该异常就中断程序的执行。,12,12,13,13,6.2 异常类的层次结构,Java中的异常类具有层次结构组织。如图所示。,14,14,6.2.1. Exception异常类的子类,(1)RuntimeException运行时异常类(2)NoSuchMethodException方法未找到异

7、常(3)java.awt.AWTException图形界面异常类(4)java.io.IOException输入输出异常类(5)Exception异常类的其它子类,15,15,(1)RuntimeException类主要包括以下异常子类:,ArithmeticException算术异常类;ArrayStoreException数组下标越界异常类;ClassCastException类型强制转换异常类;IndexOutOfBoundsException下标越界异常类;NullPointerException空指针异常类;SecurityException违背安全原则异常类。,16,16,(2)j

8、ava.io.IOException类的子类有:,IOException:申请I/O操作没有正常完成。EOFException:在输入操作正常结束前遇到了文件结束符。FileNotFoundException:在文件系统中,没有找到由文件名字符串指定的文件。,17,17,(3)Exception异常类的其它子类:,EmptyStackException;NoSuchFieldException;NoSuchMethodException;ClassNotFoundException;CloneNotSupportedException;IllegalAccessException;Instan

9、tiationException;InterruptedException。,18,18,6.2.2 Error错误类的子类,(1)VirtualMachineError虚拟机错误OutOfMemoryError内存溢出错误StackOverflowError栈溢出错误(2)LinkageError链接错误(3)NoClassDefNotFoundError类定义未找到错误(4)java.awt.AWTError图形界面错误,19,19,6.3 异常的处理,异常处理语句格式为: try / 被监视的代码段,一旦发生异常,则交由其后的catch代码段处理 catch (异常类型 e) / 要处理

10、的第一种异常 catch (异常类型 e) / 要处理的第二种异常 finally / 最终处理,20,20,public class ExTestpublic static void main(String args) String friend=lisa,bily,mary;tryfor (int i=0;i5;i+ )System.out.println(friendi);catch (java.lang.ArrayIndexOutOfBoundsException e)System.out.println(index error);System.out.println(n the en

11、d!);,21,21,例6.1 使用 trycatch语句处理异常的过程。运行结果如图所示:,22,22,public class TC1 public static void main(String arg3) System.out.println(这是一个异常处理的例子n); try int i=10; i /=0; catch (ArithmeticException e) System.out.println(异常是:+e.getMessage(); finally System.out.println(finally 语句被执行); ,23,23,例6.2 catch语句中声明的异常

12、类型不匹配的情况。,public class TC2 public static void main(String args) System.out.println(“这是一个异常处理的例子n”); try int i=10; i /=0; catch (IndexOutOfBoundsException e) System.out.println(异常是:+e.getMessage(); finally System.out.println(finally 语句被执行); ,24,24,例6.3 多个catch子句的异常处理。运行结果如图所示。,25,25,public class TC3

13、public static void main(String args) try int a=args.length; System.out.println(na = +a); int b=42/a; int c=1; c42=99; ,26,26,catch (ArithmeticException e) System.out.println(发生了被 0 除:+e); catch (ArrayIndexOutOfBoundsException e) System.out.println(数组下标越界:+e); ,27,27,6.3.2 throw语句,throw语句格式为: ;程序会在th

14、row语句处立即终止,转向trycatch寻找异常处理方法,不再执行throw后面的语句。下面的例子中使用了throw语句主动抛出一个异常。,28,28,例6.4 throw语句的使用,运行结果如图所示。,29,29,public class TC5 static void throwProcess() try throw new NullPointerException(空指针异常); catch (NullPointerException e) System.out.println(n在 throwProcess 方法中捕获一个+e.getMessage(); throw e; public static void main(String args) try throwProcess(); catch (NullPointerException e) System.out.println(再次捕获:+e); ,

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

当前位置:首页 > 行业资料 > 其它行业文档

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