java异常



(1)、try-catch


实例:


[java]  view plain copy
  1. public class TestException {    
  2.     public static void main(String[] args) {    
  3.         int[] intArray = new int[3];    
  4.         try {    
  5.             for (int i = 0; i <= intArray.length; i++) {    
  6.                 intArray[i] = i;    
  7.                 System.out.println("intArray[" + i + "] = " + intArray[i]);    
  8.                 System.out.println("intArray[" + i + "]模 " + (i - 2) + "的值:  "    
  9.                         + intArray[i] % (i - 2));    
  10.             }    
  11.         } catch (ArrayIndexOutOfBoundsException e) {    
  12.             System.out.println("intArray数组下标越界异常。");    
  13.         } catch (ArithmeticException e) {    
  14.             System.out.println("除数为0异常。");    
  15.         }    
  16.         System.out.println("程序正常结束。");    
  17.     }    
  18. }    

上面的代码中捕获两个错误:除数为0和数组下标越界异常。运行结果

(1)、try-catch


实例:


[java]  view plain copy
  1. public class TestException {    
  2.     public static void main(String[] args) {    
  3.         int[] intArray = new int[3];    
  4.         try {    
  5.             for (int i = 0; i <= intArray.length; i++) {    
  6.                 intArray[i] = i;    
  7.                 System.out.println("intArray[" + i + "] = " + intArray[i]);    
  8.                 System.out.println("intArray[" + i + "]模 " + (i - 2) + "的值:  "    
  9.                         + intArray[i] % (i - 2));    
  10.             }    
  11.         } catch (ArrayIndexOutOfBoundsException e) {    
  12.             System.out.println("intArray数组下标越界异常。");    
  13.         } catch (ArithmeticException e) {    
  14.             System.out.println("除数为0异常。");    
  15.         }    
  16.         System.out.println("程序正常结束。");    
  17.     }    
  18. }    

上面的代码中捕获两个错误:除数为0和数组下标越界异常。运行结果

intArray[0] = 0

intArray[0]模 -2的值:  0

intArray[1] = 1

intArray[1]模 -1的值:  0

intArray[2] = 2

除数为0异常。

程序正常结束



[java]  view plain copy
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">package Test;    
  2. import java.lang.Exception;    
  3. public class TestException {    
  4.     static int quotient(int x, int y) throws MyException { // 定义方法抛出异常    
  5.         if (y < 0) { // 判断参数是否小于0    
  6.             throw new MyException("除数不能是负数"); // 异常信息    
  7.         }    
  8.         return x/y; // 返回值    
  9.     }    
  10.     public static void main(String args[]) { // 主方法    
  11.         int  a =3;    
  12.         int  b =0;     
  13.         try { // try语句包含可能发生异常的语句    
  14.             int result = quotient(a, b); // 调用方法quotient()    
  15.         } catch (MyException e) { // 处理自定义异常    
  16.             System.out.println(e.getMessage()); // 输出异常信息    
  17.         } catch (ArithmeticException e) { // 处理ArithmeticException异常    
  18.             System.out.println("除数不能为0"); // 输出提示信息    
  19.         } catch (Exception e) { // 处理其他异常    
  20.             System.out.println("程序发生了其他的异常"); // 输出提示信息    
  21.         }    
  22.     }    
  23.     
  24. }    
  25. class MyException extends Exception { // 创建自定义异常类    
  26.     String message; // 定义String类型变量    
  27.     public MyException(String ErrorMessagr) { // 父类方法    
  28.         message = ErrorMessagr;    
  29.     }    
  30.     
  31.     public String getMessage() { // 覆盖getMessage()方法    
  32.         return message;    
  33.     }    
  34. } </span>  
要注意的是,throw 抛出的只能够是可抛出类Throwable 或者其子类的实例对象。

要注意的是,throw 抛出的只能够是可抛出类Throwable 或者其子类的实例对象

(1)、try-catch


实例:


[java]  view plain copy
  1. public class TestException {    
  2.     public static void main(String[] args) {    
  3.         int[] intArray = new int[3];    
  4.         try {    
  5.             for (int i = 0; i <= intArray.length; i++) {    
  6.                 intArray[i] = i;    
  7.                 System.out.println("intArray[" + i + "] = " + intArray[i]);    
  8.                 System.out.println("intArray[" + i + "]模 " + (i - 2) + "的值:  "    
  9.                         + intArray[i] % (i - 2));    
  10.             }    
  11.         } catch (ArrayIndexOutOfBoundsException e) {    
  12.             System.out.println("intArray数组下标越界异常。");    
  13.         } catch (ArithmeticException e) {    
  14.             System.out.println("除数为0异常。");    
  15.         }    
  16.         System.out.println("程序正常结束。");    
  17.     }    
  18. }    

上面的代码中捕获两个错误:除数为0和数组下标越界异常。运行结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值