Java基础:异常处理

示例代码:

  1. public class ExecDemo {
  2.     public static void main(String[] args) {     
  3.         int[] nums = new int[4];
  4.         System.out.println("before the exception:");
  5.         try {                                                                     //try代码块   try catch代码块可以嵌套
  6.             try{                  
  7.                  nums[7] = 10;                                               //数组越界
  8.                  System.out.println("no exception:");
  9.             }catch(ArithmeticException e){
  10.                  e.printStackTrace();
  11.             }
  12.         }catch(ArithmeticException e) {  //catch代码块        多个catch块
  13.             e.printStackTrace();                                         //打印异常信息
  14.         }catch(ArrayIndexOutOfBoundsException e){       // 捕获数组越界错误    捕获子类异常
  15.               e.printStackTrace();
  16.        }catch(Throwable e){                                                //捕获超类异常 Throwable是所有异常的超类
  17.               e.printStackTrace();
  18.        }
  19.         System.out.println("after the exception");
  20.     }
  21. }
    ------------------------------------------------------------------------------------------------
  22. 抛出异常:
  23.  
  24. public class ThrowDemo {
  25.     public static void main(String[] args) {
  26.         try {
  27.             System.out.println("before throw:");
  28.             throw new ArithmeticException();    //throw关键字抛出一个ArithmeticException()异常(手动抛出异常)
  29.         } catch (ArithmeticException e) {                 //捕获异常
  30.             System.out.println("exception caught:");
  31.         }        
  32.         System.out.println("after try{}catch{}:");
  33.     }
  34. }
  35. ----------------------------------------------------------------------------
  36. 重新抛出异常:
  37. class Rethrow {
  38.     public static void genException() {
  39.       
  40.         int[] numer = {2,4,6,8,10,12};
  41.         int[] demon = {2,0,3,4,0};
  42.                 
  43.             for(int i=0;i < numer.length;i++){
  44.                 try {                                                                      //try代码块
  45.                     System.out.println(numer[i]/demon[i]);
  46.                 } catch (ArithmeticException e) {                    //多个catch()块
  47.                     System.out.println("can't dev by zero:");
  48.                 }catch(ArrayIndexOutOfBoundsException e) {
  49.                     System.out.println("no error:");
  50.                     throw e;                                        //throw 重写抛出异常
  51.                 }
  52.             }        
  53.     }
  54. }
  55. public class RethrowDemo{
  56.     public static void main(String args[]) {
  57.         try {
  58.             Rethrow.genException();
  59.         } catch (ArrayIndexOutOfBoundsException e) {        //捕获重新抛出的异常
  60.             System.out.println("error error error error error:");            
  61.         }
  62.         finally{                                                 //finally代码块在try catch执行完时执行的。
  63.               System.out.println("Leaving try.");
  64.          }
  65.     }    
  66. }
  67. --------------------------------------------------------------------------------------
  68. throws语句:一个方法产生自己不做处理的异常,用throws抛出到外层(谁调用,谁处理异常)
  69.  
  70. public class ThrowsDemo {
  71.     public static char prompt(String str) throws java.io.IOException{//prompt()方法产生自己不做处理的IOException异常,抛出到外层,谁调用谁处理异常
  72.         System.out.print(str +":");
  73.         return (char) System.in.read();
  74.         
  75.     }
  76.     public static void main(String[] args) {    
  77.         char ch;
  78.         try {
  79.             ch = prompt("enter a letter");          //prompt()可能抛出异常,
  80.         } catch (java.io.IOException e) {        //捕获prompt()抛出的异常
  81.            
  82.             System.out.println("IOException occurred");
  83.             ch = 'x';
  84.         }
  85.         System.out.println("you pressed:"+ ch);
  86.     }
  87. }
    ------------------------------------------------------------------------------------
  88. 可以用一个catch()捕获多个异常:
  89. try{
  90.  
  91. }
  92. catch(ArithmeticException|ArrayIndexOutOfBoundsException e){//同时捕获多个异常
  93. }
  94. -----------------------------------------------------------------------------------
  95. 自定义异常:
  96. class NonIntResultException extends Exception{     //自定义异常继承子Exception
  97.     int n ,d;
  98.     NonIntResultException(int i,int j){
  99.         n = i;
  100.         d = j;
  101.     }   
  102.     public String toString() {
  103.         return "result of "+ n +"/"+ d +" is non-integer.";
  104.         
  105.     }
  106. }
  107. public class CustomExceptionDemo {
  108.  
  109.     public static void main(String[] args) {
  110.        
  111.         int numer[] = {4,8,15,32,64,127,256,512};
  112.         int denom[] = {2,0,4,4,0,8};
  113.         
  114.         for(int i=0;i<numer.length;i++) {
  115.             try {
  116.                 if((numer[i]%2)!=0) {
  117.                     throw new NonIntResultException(numer[i],denom[i]);   //抛出自定义异常
  118.                 }
  119.                 System.out.println(numer[i] +"/"+ denom[i] +" is "+ numer[i]/denom[i]);
  120.             } catch (ArithmeticException e) {                      //捕获ArithmeticException异常            
  121.                 System.out.println("can't divide by zero!");
  122.             }catch (ArrayIndexOutOfBoundsException e) {  //捕获ArrayIndexOutOfBoundsException 异常           
  123.                 System.out.println("no matching element found.");
  124.             }catch (NonIntResultException e) {                 //捕获自定义异常           
  125.                 System.out.println(e);
  126.             }
  127.         }
  128.         
  129.     }
  130. }

     

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/87950322