java exception api_Java API —— 异常

packagedemo01;/*** Created by gao on 15-12-24.*/

/** A:一个异常

* B:二个异常的处理

* a:每一个写一个try...catch

* b:写一个try,多个catch

* try{

* ...

* }catch(异常类名 变量名) {

* ...

* }

* catch(异常类名 变量名) {

* ...

* }

* ...

*

* 注意事项:

* 1:能明确的尽量明确,不要用大的来处理。

* 2:平级关系的异常谁前谁后无所谓,如果出现了子父关系,父必须在后面。

*

* 注意:

* 一旦try里面出了问题,就会在这里把问题给抛出去,然后和catch里面的问题进行匹配,

* 一旦有匹配的,就执行catch里面的处理,然后结束了try...catch

* 继续执行后面的语句。*/

public classExceptionDemo02 {public static voidmain(String[] args) {//一个异常//method1();//两个异常//method2();//两个异常的处理//method3();//使用Exception作为终极处理

method4();

}public static voidmethod1(){//第一阶段

int a = 10;//int b = 2;

int b = 0;try{

System.out.println(a/b);

}catch(ArithmeticException e){

System.out.println("除数不能为0");

}//第二阶段

System.out.println("over");

}public static voidmethod2(){int a = 10;int b = 0;try{

System.out.println(a/b);

}catch(ArithmeticException e){

System.out.println("除数不能为0");

}int[] arr = { 1, 2, 3};try{

System.out.println(arr[3]);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("你访问了不该的访问的索引");

}

System.out.println("over");

}public static voidmethod3(){int a = 10;int b = 0;int[] arr = { 1, 2, 3};try{

System.out.println(arr[3]);

System.out.println(a/b);

}catch(ArithmeticException e){

System.out.println("除数不能为0");

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("你访问了不该的访问的索引");

}

System.out.println("over");

}public static voidmethod4(){int a = 10;int b = 0;int[] arr = { 1, 2, 3};try{

System.out.println(a/b);

System.out.println(arr[3]);

System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");

}catch(ArithmeticException e) {

System.out.println("除数不能为0");

}catch(ArrayIndexOutOfBoundsException e) {

System.out.println("你访问了不该的访问的索引");

}catch(Exception e) {

System.out.println("出问题了");

}

System.out.println("over");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值