Java2第9章 异常处理

上传人:野鹰 文档编号:3033477 上传时间:2017-07-29 格式:PPT 页数:29 大小:274.50KB
返回 下载 相关 举报
Java2第9章 异常处理_第1页
第1页 / 共29页
Java2第9章 异常处理_第2页
第2页 / 共29页
Java2第9章 异常处理_第3页
第3页 / 共29页
Java2第9章 异常处理_第4页
第4页 / 共29页
Java2第9章 异常处理_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《Java2第9章 异常处理》由会员分享,可在线阅读,更多相关《Java2第9章 异常处理(29页珍藏版)》请在金锄头文库上搜索。

1、1,教学内容: 异常处理的机制 捕获与处理异常 多异常处理 抛出异常 自定义异常类 重点: 异常处理的机制 捕获与处理异常难点: 自定义异常类学时:3,第9章异常处理,2,9.1.1 错误与异常,运行错误:系统运行错误和逻辑运行错误。系统运行错误简称为错误,是指程序在执行过程中所产生对操作系统的损害。 逻辑运行错误是指程序不能实现程序员的设计意图和设计功能而产生的错误,这种错误也被称为异常。,9.1 异常处理的基本概念,3,9.1.2 Java异常处理机制,Java异常处理机制,简单地说,就是程序在运行时,发现异常的代码可以“抛出”一个异常,运行系统“捕获”该异常,并交由程序员编写的相应代码进

2、行异常处理。,Public class ExceptionDemo public static void main(String args) int a=0; System.out.println(8/a); ,jvm: 产生异常对象 抛出异常对象java 程序: 捕获异常(catch) 处理异常,4,9.2 异常处理类,Java语言中定义了很多异常类,而每个异常类都代表一种运行错误Java的异常类是处理运行时错误的特殊类,类中包含了该运行错误的信息和处理错误的方法等内容。 程序对错误与异常的处理方式有三种:程序不能处理的错误;程序应避免而不捕获的运行时异常;必须捕获的非运行时异常。,5,异常

3、类的层次和主要子类: Error : 一般不能由java程序处理,如jvm 发生错误等Exception:受检异常:编译能检测到,在程序中必须进行异常处理。(如I/O操作)非受检异常:编译时不能检测到。称运行时异常(RuntimeException )。 在程序中可以处理,也可以不处理异常。 (如除 0),Object,Throwable,Error,Exception,RuntimeException,ArithmeticException,。,IOException,6,RuntimeException:可以处理也可以不处理不处理,系统会捕获异常,并停止程序。ArithmeticExcep

4、tion : /0 用0取模ArrayIndexOutBoundsExceptionNullPointerException:访问空对象,7,9.3 捕获与处理异常,在Java语言中,异常处理是通过try、catch、finally、throw、throws五个关键字来实现的。,public class app9_1 /app9_1.java 异常的产生 public static void main(String args) int i; int a=1,2,3,4; /定义并初始化数组 for (i=0;i5;i+) System.out.println(“ a“+i+”=”+ai); S

5、ystem.out.println(“5/0“+(5/0); ,8,在Java的异常处理机制中,提供了try-catch-finally语句来捕获和处理一个或多个异常,其语法格式如下:,try catch (异常类名1 对象名1) catch (异常类名2 对象名2) finally ,9,public class ExceptionDemo public static void main(String args) int a=0; try System.out.println(8/a); System.out.println(“成功”); catch(ArithmeticException

6、e) System.out.println(e.toString(); finally System.out.println(“程序结束”); ,一定会被执行,10,class ExceptionDemo1 /多catch public static void main(String args) int n1,n2; String s1,s2; try InputStreamReader sr= new InputStreamReader (System.in); BufferedReader br = new BufferedReader( sr); s1 = br.readLine();

7、/被除数 s2 = br.readLine(); /除数 n1 = Integer.parseInt( s1) ; n2 = Integer.parseInt( s2) ; System.out.println( n1/ n2 ); System.out.println(“成功”); catch(ArithmeticException e) System.out.println( “除数为 0”); catch(IOException e) System.out.println( “输入异常”); finally System.out.println(“程序结束”); ,11,public c

8、lass app9_2 /异常的捕获与处理 public static void main(String args) int a=1,2,3,4; for (int i=0;i5;i+) try System.out.print(“a”+i+”/”+i+”=”+(ai/i); catch(ArrayIndexOutOfBoundsException e) System.out.print(“捕获到了数组下标越界异常”); catch(ArithmeticException e) System.out.print(“异常类名称是:”+e); /异常信息 catch(Exception e) Sy

9、stem.out.println(“捕获”+e.getMessage() ); finally System.out.println(“ finally i=”+i); System.out.println(“继续!”); ,12,程序运行结果为:异常类名称是:java.lang.ArithmeticException:/ by zero finally i=0a1/1=2 finally i=1a2/2=1 finally i=2a3/3=1 finally i=3捕获到了数组下标越界异常 finally i=4继续!,13,9.4 抛出异常,根据异常类的不同,抛出异常的方法也不相同。(1)

10、系统自动抛出的异常。(2)使用throw语句抛出的异常。 throw 由异常类所产生的对象; (3)抛出异常的方法与调用方法处理异常。(4)由方法抛出异常交系统处理。,14,(1、2)程序产生异常 - throw 语句用来明确地由Java 程序产生一个异常, throw 异常 ;,import java.io.*;public class tryDemo public static void main(String args) try System.out.println( 1111 ) ; throw new IOException( IO error); catch( IOExceptio

11、n e) System.out.println( IO异常 ); finally System.out.println( end ); ,15,public class app9_3 /使用throw语句在方法之中抛出异常 public static void main(String args) int a=5,b=0; try if (b=0) throw new ArithmeticException(); /抛出异常 else System.out.println(a+“/“+b+”=”+a/b); catch(ArithmeticException e) System.out.prin

12、tln(“异常:”+e+” 被抛出了!”); ,16,(3) 抛出异常的方法与调用方法处理异常。,某方法中存在受检异常,但不处理异常,那么必须交给调用该方法的方法处理: 返回类型 方法名( 形参) throws ExceptionList 方法体 说明:异常可以向上一级调用方法传递ExceptionList 可以多个异常类,用 “,”分隔,17,/转移异常class throwsDemo static void fun() throws ArithmeticException int a=0; System.out.println( 8/a); public static void main(String args ) try fun(); ) catch( ArithmeticException e) System.out.ptinyln(“异常”); ,main,fun,18,/程序产生异常2class throwsDemo static void fun() throws IOException throw new IOException( IO error); public static void main(String args) try fun(); catch( IOException e) System.out.println( e.toString() ); ,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


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

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