异常处理

来源:互联网 发布:java asm 中文文档 编辑:程序博客网 时间:2024/06/05 04:25
public class A{
    public static void main (String [] args)
    {
        int i=0;
        int j=0;
        System.out.println("=======计算开始啦========");
        try
        {
            i=Integer.parseInt(args[0]);
            j=Integer.parseInt(args[1]);
            int temp=i/j;
            System.out.println("计算结果: "+temp);
        }
        catch(ArithmeticException e)
        {
            System.out.println("出现了数字异常: "+e); //验证是否除数为 0 
        }
        
        catch(NumberFormatException e)
        {
            System.out.println("输入的不是数字: "+e); //验证是否输入了正确格式(数字)
        }
        
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("输入的个数不对: "+e);  //捕获输入的个数,下标是否越界
        }
        catch(Exception e)
        {
            System.out.println("其它异常: "+e);
        }
        finally
        {
            System.out.println("****不管是否有异常,我都输出******");
        }
        System.out.println("=======计算结束啦========");
    }
}

0 0
原创粉丝点击