天天看点

建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。

package exception;

public class TestException {

    public static void main(String[] args) {
        
        for(int i=0;i<4;i++)
        {
            try
        {
            int  k;
            switch(i)
            {
                
                case 0:
                int zero= 0;
                k=911/zero;
                break;                
            
                case 1:
                int  b[]=null;
                k = b[0];
                break;
                
                case 2:
                int c[]=new int[2];
                k=c[9];
                break;
                
                case 3:
                char  ch="abc".charAt(99);
                break;
            }
        }
        
        catch(ArithmeticException e)
        {
            System.out.println(" 错误 1"+e.getMessage());
            e.printStackTrace();            
        }
        catch(NullPointerException e)
        {
            System.out.println(" 错误2 "+e.getMessage());
            e.printStackTrace();
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println(" 错误3 "+e.getMessage());
            e.printStackTrace();
            
        }
        catch(Exception e)
        {  
            System.out.println(" 错误4 "+e.getMessage());
            e.printStackTrace();
            
        }
        
    
        }    
}
}