1、什么是异常,运行时发生的错误称为异常。处理这些异常就称为异常处理。 一旦引发异常,程序将突然中止,且控制将返回操作系统。 发生异常后此前分配的所有资源都将保留在相同的状态,这将导致资源漏洞。,Java异常处理基础,Java异常处理机制采用一个统一和相对简单的抛出和处理错误的机制。如果一个方法本身能引发异常,当所调用的方法出现异常时,调用者可以捕获异常使之得到处理;也可以回避异常,这时异常将在调用的堆栈中向下传递,直到被处理。,异常体系结构,所有异常类型都是 内置类Throwable的子类,用于Java运行时系统 来显示与运行时系 统本身有关的错误,用于用户程序可能 捕获的异常,也是用来创建用户
2、异常类型子类的类。,Error类对象由Java虚拟机生成并抛出; Exception类对象由应用程序处理或抛出。,常见异常及其用途,常见的异常及其用途,JAVA异常处理方法,JAVA程序用try-catch(或try-catch-finally)语句来抛出、捕捉和处理异常。 如果程序运行时出现了系统定义的异常,系统会自动抛出。此时,若程序中有try-catch语句,则这些异常由系统捕捉并交给应用程序处理;若程序中没有try-catch语句,则这些异常由系统捕捉和处理,此时需用到throw、throws。 Java 中可用于处理异常的两种方式: 自行处理:可能引发异常的语句封入在 try 块内,
3、而处理异常的相应语句则封入在 catch 块内。 回避异常:在方法声明中包含 throws 子句,通知潜在调用者,如果发生了异常,必须由调用者处理。,try - catch 块示例,public class ExceptionDemo public static void main(String args) try int c= calculate(9,0); System.out.println(c); catch (Exception e) System.err.println(发生异常: + e.toString(); e.printStackTrace(); static int ca
4、lculate(int a, int b) int c = a/b; return c; ,调用函数calculate, 将引发一个异常,在catch块中处理异常,输出 结果,多个 catch 块,单个代码片段可能会引起多个错误。 可提供多个 catch 块分别处理各种异常类型。,. . . try catch(ArrayIndexOutOfBoundsException e) catch(Exception e) .,ArrayIndexOutOfBoundsException类为 Exception 类的子类,但是如果异常 属于ArrayIndexOutOfBoundsException类
5、将执行第一个 catch 块,之后控制 将转向try/catch块之后的语句,所以始终不会执行第二个 catch 块。,多个 catch 块示例,class Catch22 public static void main(String args) try String num=args0; int numValue=Integer.parseInt(num); System.out.println(平方为 +numValue*numValue); catch(ArrayIndexOutOfBoundsException ne) System.out.println(未提供任何参数!); cat
6、ch(NumberFormatException nb) System.out.println(不是数字!); ,嵌套的 try - catch 块,有时,块的一部分引起一个错误,而整个块可能又引起另一个错误。在此情况下,需要将一个异常处理程序嵌套到另一个中。 在使用嵌套的try块时,将先执行内部 try 块,如果没有遇到匹配的 catch 块,则将检查外部 try 块的 catch 块。,finally 块,确保了在出现异常时所有清除工作都将得到处理 与 try 块一起使用 无论是否出现异常,finally块都将运行,finally 块 示例,class FinallyDemo int no
7、1,no2; FinallyDemo(String args) try no1 = Integer.parseInt(args0); no2 = Integer.parseInt(args1); System.out.println(相除结果为 +no1/no2); catch(ArithmeticException i) System.out.println(不能除以 0); finally System.out.println(Finally 已执行); public static void main(String args) new FinallyDemo(args); ,练习: 编写J
8、ava程序,包含三种异常:算术异常, 字符串越界,数组越界。 观察输出信息:每个异常对象可以直接给出信息,class first_exception public static void main(String args) char c; int a,b=0; int array=new int7; String s=Hello;,try a=1/b; catch(ArithmeticException ae) System.out.println(“Catch “+ae);,try array8=0; catch(ArrayIndexOutOfBoundsException ai) Syst
9、em.out.println(“Catch “+ai);,try c=s.charAt(8); catch(StringIndexOutOfBoundsException se) System.out.println(“Catch “+se);, ,异常的抛出和处理,异常是通过关键字 throw 抛出,程序可以用throw语句引发明确的异常。如: try if(flagjview throwsException In Situation 0 no Exception caught In Situation 1 Catch java.lang.ArrayIndexOutOfBoundsExcep
10、tion:10,使用 throws示例,class ThrowsDemo static void throwOne() throws IllegalAccessException System.out.println(在throwOne中.); throw new IllegalAccessException(非法访问异常); public static void main(String args) try throwOne(); catch(IllegalAccessException e) System.out.println(捕获+e); ,在该方法中没有处 理异常,只是声明 可能引发的
11、异常,在throwOne方法 的调用函数中捕 获并处理异常,用户自定义的异常,内置异常不可能始终足以捕获所有错误,因此需要用户自定义的异常类 用户自定义的异常类应为 Exception 类(或者Exception 类的子类)的子类 创建的任何用户自定义的异常类都可以获得 Throwable类定义的方法,class ArraySizeException extends NegativeArraySizeException ArraySizeException() super(“您传递的是非法的数组大小”); ,该类是Exception的子类,使用用户自定义的异常示例,class UserExce
12、ptionDemo int size, array; UserExceptionDemo(int s) size = s; try checkSize(); catch(ArraySizeException e) System.out.println(e); void checkSize() throws ArraySizeException if(size 0) throw new ArraySizeException(); array = new intsize; for(int i = 0; i size; i+) arrayi = i+1; System.out.print(array
13、i+ ); public static void main(String arg) new UserExceptionDemo(Integer.parseInt(arg0); ,总结,运行时发生的错误称为异常。 必须捕获引发的每个异常,否则应用程序不会正常中止。 异常处理允许在一个地方集中进行错误处理。这使得可以创建功能强大且健壮的代码。 Java 使用 try 和 catch 块来处理异常。try 块中的语句引发异常,而 catch 块则处理异常。,总结,可以同时使用多个 catch 块来分别处理各种异常类型。 程序可以用throw语句引发明确的异常。 关键字 throws 用于列出一个方法可能引发的异常类型。 不管是否发生了异常,都将执行 finally 块中的语句。,