day_16异常处理

星期二, 十一月 17, 2015 09:43:59

一、异常的基本概念

代码案例:

package day16;

public class ExceptionTest {

public static void main(String[] args) {

int a[] = new int[5];

/*用非法索引访问数组时抛出的异常。

* 如果索引为负或大于等于数组大小,则该索引为非法索引。

* java.lang.ArrayIndexOutOfBoundsException*/

a[8] = 10;

System.out.print("end the main[]");

}

}

运行结果:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8

at day16.ExceptionTest.main(ExceptionTest.java:9)

注解:

    如果没有编写相应的处理异常的程序代码,Java的默认处理机制会先抛出异常,然后停止运行程序。

二、异常的处理

    2.1如果加上捕捉异常的程序代码,则可针对不同的异常做处理--->异常处理

    2.2格式:

     try{

     要检查的程序语句;

     ...

     }catch(异常类 对象名称){

       异常发生时的处理语句;

     }final{

       一定会运行到的程序代码;

     }

     2.3代码案例:

package day16;

public class ExceptionTest {

public static void main(String[] args) {

try {

int a[] = new int[5];

a[8] = 10;

} catch (ArrayIndexOutOfBoundsException e) {

  /*e.printStackTrace();*/

System.out.println("数组超出绑定范围");

}finally{

System.out.println("end the main{}");

}

}

}

     运行结果:

     数组超出绑定范围

     end the main{}

2.4 catch异常类之后生成一个对象e,利用此对象可以得到异常的相关信息。

代码案例:

package day16;

public class ExceptionTest {

public static void main(String[] args) {

try {

int a[] = new int[5];

a[8] = 10;

} catch (ArrayIndexOutOfBoundsException e) {

//    e.printStackTrace();

System.out.println("数组超出绑定范围");

System.out.println("e   "+ e);

}finally{

System.out.println("end the main{}");

}

}

}

运行结果:

数组超出绑定范围

e   java.lang.ArrayIndexOutOfBoundsException: 8

end the main{}

2.5 Throwable继承关系图

 java.lang.Object

           -----java.lang.Throwable

 直接已知子类:Error

                                Exception----IOException

                                               ----RuntimeException

                                                                                -----ArithmeticException

                                                                                -----IndexOutOfBoundsException

                                                                                                                                      -----ArrayIndexOutOfBoundsException,

                                                                                                                                      ------ StringIndexOutOfBoundsException

RuntimeException即使不编写异常处理的程序代码,也可以编译成功,而这种异常必须在程序运行时才有可能发生.例如 数组的索引值超出了范围.

IOException一定要编写异常处理的程序代码才行,它通常处理与输入/输出相关的操作.如文件的访问,网络的链接等.

三、抛出异常

 throw异常,以及如何由try-catch来接收所抛出的异常。

抛出异常的方式两种:

       1. 程序中抛出异常

       2. 指定方法抛出异常

3.1 在程序中抛出异常

   关键字throw,语法:

   throw 异常类实例对象;

3.1.1 代码案例:

package day16;

public class ExceptionTest {

public static void main(String[] args) {

int a = 4, b = 0;

try {

if(b==0) {

/* public ArithmeticException(String s)构造具指定详细消息的 ArithmeticException。*/

throw new ArithmeticException("一个算数异常");

}else{

System.out.println("a = "+ a+" b = "+b+"  a/b = " +a/b);

}

} catch (ArithmeticException e) {

System.out.print("e .... "+e);

/*e.printStackTrace();*/

}

}

}

运行结果:

e .... java.lang.ArithmeticException: 一个算数异常

注解:

 抛出异常时,throw关键字所抛出的是异常类的实例对象,所以throw语句必须使用new关键字来产生对象。

3.2 指定方法抛出异常

如果方法会抛出异常,则可将处理此异常的try-catch-finally块写在调用此方法的程序代码内。

          

3.2.1代码案例:

package day16;

public class ExceptionTest {

public static void main(String[] args)  {

Test t = new Test();

try {

t.add(4,0);

} catch (Exception e) {

System.out.println("e = "+ e);

/*e.printStackTrace();*/

}

}

}

class Test {

//throws在指定方法中不处理异常,在调用此方法的地方处理

void add(int a,int b) throws Exception{

int c;

c = a/b;

System.out.println("a =" +a +"  b = "+b+"  c="+c);

}

}

运行结果:

e = java.lang.ArithmeticException: / by zero

注解:

   如果在类的方法中用throws抛出一个异常,那么在调用它的地方就必须明确地用try-catch来捕捉。

3.2.2代码案例:

package day16;

public class ExceptionTest {

public static void main(String[] args) throws Exception {

Test t = new Test();

t.add(4,0);

}

}

class Test {

//throws在指定方法中不处理异常,在调用此方法的地方处理

void add(int a,int b) throws Exception{

int c;

c = a/b;

System.out.println("a =" +a +"  b = "+b+"  c="+c);

}

}

运行结果:

Exception in thread "main" java.lang.ArithmeticException: / by zero

at day16.Test.add(ExceptionTest.java:48)

at day16.ExceptionTest.main(ExceptionTest.java:33)

注解:

public static void main(String[] args) throws Exception程序也可以编译通过,将异常方法向上传递,

而main()方法是由整个程序的起点,所以在main()方法处如果再用throws抛出异常,则此异常就将交由JVM

进行处理。

四、编写自己的异常类

4.1 Java可通过继承的方式编写自己的异常类。

语法:

class 异常名称 extends Exception {

}

在自定义异常类里编写方法来处理相关的事件,甚至不编写任何语句也可以正常工作,

因为父类Exception已提供相当丰富的方法,通过继承,子类均可使用它们。

public Exception(String message)

参数message - 详细消息。保存详细消息,以便以后通过 Throwable.getMessage() 方法获取它。

4.2 代码案例

package day16;

public class ExceptionTest {

public static void main(String[] args)  {

try {

throw new DefaultException("自定义异常");

} catch (DefaultException e) {

System.out.print("e..."+e);

/*e.printStackTrace();*/

}

}

}

/*自定义异常类*/

class DefaultException extends Exception {

public DefaultException (String msg) {

/*public Exception(String?message)

参数message - 详细消息。保存详细消息,以便以后通过 Throwable.getMessage() 方法获取它。*/

super(msg);

}

}

运行结果:

e...day16.DefaultException: 自定义异常

Exception 构造方法

public Exception(String message){}

星期二, 十一月 17, 2015 12:07:28

猜你喜欢

转载自yuzhouxiner.iteye.com/blog/2257293