java语言异常处理.ppt

上传人:laozhun 文档编号:2354868 上传时间:2023-02-15 格式:PPT 页数:26 大小:159KB
返回 下载 相关 举报
java语言异常处理.ppt_第1页
第1页 / 共26页
java语言异常处理.ppt_第2页
第2页 / 共26页
java语言异常处理.ppt_第3页
第3页 / 共26页
java语言异常处理.ppt_第4页
第4页 / 共26页
java语言异常处理.ppt_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《java语言异常处理.ppt》由会员分享,可在线阅读,更多相关《java语言异常处理.ppt(26页珍藏版)》请在三一办公上搜索。

1、1,异常和异常类异常处理创建异常,第六章 异常处理,2,软件程序肯定会发生错误/问题what really matters is what happens after the error occurs.How is the error handled?Who handles it?Can the program recover,or should it just die?从外部问题(硬盘、网络故障等)到编程错误(数组越界、引用空对象等)致命错误内存空间不足等错误(Error)导致程序异常中断程序不能简单地恢复执行非致命错误数组越界等异常(Exception)导致程序中断执行程序在修正后可恢复执

2、行,异常(Exception),3,Java语言中已定义或用户定义的某个异常类的对象一个异常类代表一种异常事件Java语言利用异常来使程序获得处理错误的能力(error-handling)异常事件(Exceptional Event)An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.,异常,4,Java语言中用来处理异常的类异常类的方法构造方法public Exception()public Exception(

3、String s)常用方法public String toString()public String getMessage()public void printStackTrace(),异常类,5,对异常的定义Java语言中定义的异常类:IOException/NullPointerException详细定义见Java文档中各个包的Exception Summary用户定义自已所需的异常类,描述程序中出现的异常结果,异常(Exception),class java.lang.Throwable class java.lang.Error(严重的问题,但不需程序捕捉的错误)class java.

4、lang.LinkageError.class java.lang.VirtualMachineError class java.lang.InternalError class java.lang.OutOfMemoryError.class java.lang.Exception(程序应该捕捉的错误)class java.lang.ClassNotFoundException class java.lang.RuntimeException(JVM正常运行时抛出的错误)class java.lang.ArithmeticException.class java.io.IOException

5、 class java.awt.AWTException class java.sql.SQLException.,6,异常和异常类异常处理创建异常,第六章 异常处理,7,当一个Java程序的方法产生一个错误,该方法创造一个异常对象并将其交给运行系统In Java terminology,creating an exception object and handing it to the runtime system is called throwing an exception(抛出异常)运行系统从错误发生处开始寻找处理错误的程序段The exception handler chosen i

6、s said to catch the exception(捕获异常)捕获异常的过程可以沿方法调用的逆向顺序寻找,异常处理,8,异常处理器(exception handler)trycatchfinally异常的抛出(throw),异常处理,9,何时会出现异常?方法中已定义了异常的抛出,异常处理,import java.io.IOException;class jex3_3 public static void main(String args)throws IOException char c;System.out.println(Please input a char:);c=(char)

7、System.in.read();System.out.println(Received char=+c);,public abstract int read()throws IOException,在程序编译的时候必须完成异常的处理,10,由于非预期的结果导致系统运行时产生异常,异常处理,class jex7_9 public static void main(String args)int a=0;int b=24/a;,java jex7_9Exception in thread main“java.lang.ArithmeticException:/by zero,class jex7

8、_9 public static void main(String args)int i=Integer.parseInt(args0);System.out.println(i);,java jex7_9Exception in thread main“java.lang.ArrayIndexOutOfBoundsException:0,java jex7_9 aException in thread main“java.lang.NumberFormatException:For input string:“a”,11,异常处理器(exception handler)trycatchfin

9、ally,异常处理,12,异常处理器(exception handler)try程序块 构造一个异常处理器,封装一些抛出异常的语句try Java 语句块;/指一个或多个抛出异常的Java语句,异常(Exception),class Test public static void main(String args)char c=(char)System.in.read();System.out.println(c);,import java.io.IOException;class Test public static void main(String args)try char c=(cha

10、r)System.in.read();System.out.println(c);catch(IOException e)System.out.println(e);,unreported exception java.io.IOException;must be caught or declared to be thrownchar c=(char)System.in.read();,一个try语句必须带有至少一个catch语句块或一个finally语句块,13,异常处理器(exception handler)try语句块定义了异常处理器的范围catch语句块捕捉try语句块抛出的异常try

11、/Code that might generate exceptions catch(Type1 id1)/Handle exceptions of Type1 catch(Type2 id2)/Handle exceptions of Type2 catch(Type3 id3)/Handle exceptions of Type3/etc.,异常处理,import java.io.IOException;class Test public static void main(String args)try char c=(char)System.in.read();System.out.pr

12、intln(c);catch(IOException e)System.out.println(e);,try.catch(ArrayIndexOutOfBoundsException e)System.out.println(e);catch(IOException e)System.out.println(e);,14,异常处理器(exception handler)finally语句块Theres often some piece of code that you want to execute whether or not an exception is thrown within a

13、 try block.finally语句块在异常处理中是必须执行的语句块清理现场关闭打开的文件关闭网络连接,异常处理,try/The guarded region:Dangerous activities/that might throw A,B,or C catch(A a1)/Handler for situation A catch(B b1)/Handler for situation B catch(C c1)/Handler for situation C finally/Activities that happen every time,15,异常的抛出在一个方法中,抛出异常,同

14、时捕捉当有多个方法调用时,由特定(适当)的方法捕捉异常被调用的方法主动抛出异常(throws),异常处理,import java.io.IOException;class Test static char getChar()throws IOException char c=(char)System.in.read();return c;public static void main(String args)try char c=getChar();System.out.println(c);catch(IOException e)System.out.println(e);,16,异常的抛出

15、主动抛出异常,异常处理,void parseObj(String s)throws NullPointerException if(s=null)throw new NullPointerException();,17,异常和异常类异常处理创建异常,第六章 异常处理,18,使用Java语言已有的异常异常的抛出/捕捉创建自已的异常异常的抛出/捕捉,创建异常,19,异常(Exception),class SimpleException extends Exception public class SimpleExceptionDemo public void f()throws SimpleExc

16、eption System.out.println(Throw SimpleException from f();throw new SimpleException();public static void main(String args)SimpleExceptionDemo sed=new SimpleExceptionDemo();try sed.f();catch(SimpleException e)System.out.println(e);System.out.println(Caught it!);,运行结果:Throw SimpleException from f()Simp

17、leExceptionCaught it!,20,自定义异常自定义异常类必须是java.lang.Exception类的子类java.lang.Exception类的两个构造方法Exception()Constructs a new exception with null as its detail message.Exception(Stringmessage)Constructs a new exception with the specified detail message.自定义异常类可以不定义构造方法SimpleException()super();自定义异常类定义自已的构造方法,

18、创建异常,21,定义自已的异常构造方法,创建异常,22,class MyException extends Exception private int x;public MyException()public MyException(String msg)super(msg);public MyException(String msg,int x)super(msg);this.x=x;,class ExtraFeatures public static void f()throws MyException System.out.println(Throws MyException from

19、f();throw new MyException();public static void g()throws MyException System.out.println(Throws MyException from g();throw new MyException(Originated in g();public static void h()throws MyException System.out.println(Throws MyException from h();throw new MyException(Originated in h(),47);,try f();cat

20、ch(MyException e)System.out.println(e);,Throwing MyException from f()MyException,try g();catch(MyException e)System.out.println(e);,Throwing MyException from g()MyException:Originated in g(),Throwing MyException from h()MyException:Originated in h(),try h();catch(MyException e)System.out.println(e);

21、,23,注意:以下的代码合法吗?,异常(Exception),try.finally.,答案:它是合法的.一个try 语句当它存在finally 子句时可以省略 catch 语句.如果 try 子句有多个出口,却没有catch子句与其连接时,不论是否捕获异常 finally 子句都要执行.,24,注意问题:下面的处理可以捕获哪些异常?,异常(Exception),catch(Exception e).,回答:这个处理捕获的是Exception类的异常;因此,它可以捕获所有的各种异常.这种执行会让你失去抛出异常的重要信息,使得你的代码的有效性降低.所以,运行系统被迫自己决定使用哪种更有效的策略去恢复异常.,25,注意:问题:下面代码可以捕获哪种异常?,异常(Exception),.catch(Exception e).catch(ArithmeticException a).,回答:第一种异常将被捕获;因为,它可以捕获所有的各种异常,包括ArithmeticException.第二种异常永远也不能捕获到,所以这个代码不会编译.,26,第六章 结束!,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号