异常处理1

package c;
/*异常处理第二部分
 *   1 语句格式
 *   try{
 *   语句组1
 *   }
 *   catch(异常类1 对象名)
 *   {
 *   语句组2
 *   }
 *   catch(异常类2 对象名)
 *   {
 *   语句组3
 *   }
 *   ...
 *   finally{
 *   语句组4
     }
    注:catch语句块的异常对象封装了异常事件发生的信息,在catch语句块中可以使用异常对象的方法
    来获得这些信息;
    getMessage():返回异常信息
    printStackTrace():用来跟踪异常事件发生时执行堆栈的内容
   
  finally语句块为异常处理提供了一个统一的出口,使得在控制流程转到其他部分以前,能够对
  程序的状态做统一的管理。通常在finally语句块中进行资源清除工作,如关闭打开的文件,清除临时
  文件。无论try文件是否抛出异常,finally语句块总要执行,finally语句不被执行的唯一情况是程序
  先执行了System.exit()。如果catch代码块和finally代码块并存,finally代码块必须放在catch代码块后面。
  */


/*
public class Exceptions1 {
public static void main(String args[]){
    int x[]={4,2,0};
    try{
        System.out.println("商1:"+x[0]/x[1]);
        x[3]=100;
        System.out.println("商2:"+x[1]/x[2]);
    }catch(ArithmeticException e)
    {System.out.print("一");
    }
    catch(RuntimeException e){
        System.out.println("二");
    }
    catch(Exception e){
        System.out.println("三");
    }
    finally{
        System.out.println("四");
    }
    System.out.println("五");
}
}
商1:2



*/

//未捕获异常
/*
public class Exceptions1{
    public static void main(String args[]){
        int x[]={4,2,0};
        try{
            System.out.println("商1:"+x[0]/x[1]);
            x[3]=100;
            System.out.println("商2"+x[1]/x[2]);
        }catch(NullPointerException e){
            System.out.println("一");
        }catch(ArithmeticException e){
            System.out.println("二");
        }finally{
            System.out.println("四");
        }System.out.println("五");
    }
}
结果:商1:2
            四
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at c.Exceptions1.main(Exceptions1.java:66)

*/


/*
public class Exceptions1{
    public static void main(String args[]){
        int x[]={4,2,0};
        try{
            System.out.println("商1:"+x[0]/x[1]);
            x[3]=100;
            System.out.println("商2:"+x[1]/x[2]);
        }catch(ArithmeticException e){
            System.out.println("一");
        }catch(RuntimeException e){
            System.out.println("二");
            throw e;
        }catch(Exception e){
            System.out.println("三");
        }
        finally{System.out.println("四");
    } System.out.print("五");
}
}
商1:2


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at c.Exceptions1.main(Exceptions1.java:92)
第二个catch语句已经将异常捕获,但由throw又抛出一个异常,所欲新的异常无法捕获,程序中断
*/
public class Exceptions1{
    public static void main(String args[]){
        int x[]={4,2,0};
        try{
            System.out.println("商1:"+x[0]/x[1]);
            x[3]=100;
            System.out.println("商2:"+x[1]/x[2]);
        }catch(ArithmeticException e){
            System.out.println("一");
        }catch(RuntimeException e){
            System.out.println("二");
            //return;
            System.exit(1);//停止虚拟机运行
        }catch(Exception e){
            System.out.println("三");
        }
        finally{System.out.println("四");
    } System.out.print("五");
    }
}
/*商1:2


*/
/*
 * 商1:2

 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值