我是靠谱客的博主 自然柚子,这篇文章主要介绍《java程序设计基础》异常的处理,现在分享给大家,希望可以做个参考。

异常的产生
输入一个数组的所有元素,捕捉数组下标越界异常和除数为0异常
package practice;

public class y {

复制代码
1
2
3
4
5
6
7
8
9
10
public static void main(String[] args) { // TODO Auto-generated method stub int i; int []a = {1,2,3,4}; for(i=0;i<5;i++){ System.out.println(" a["+i+"]="+a[i]); System.out.println("5/0"+(5/0)); } }

}
//一般使用 try -catch -finally 来进行异常的处理
异常的处理
package practice;

public class yy {

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static void main(String[] args) { // TODO Auto-generated method stub int i ; int []a = {1,2,3,4}; for(i=0;i<5;i++) { try { System.out.println("a["+i+"]/"+i+"="+(a[i]/i)); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("捕捉到数组下标越界异常"); } catch(ArithmeticException e) { System.out.println("异常类的名称为"+e); } catch(Exception e) { System.out.println("捕获"+e.getMessage()+"异常!"); } finally { System.out.println(" finally i="+i); } } System.out.println("继续!!"); }

}

最后

以上就是自然柚子最近收集整理的关于《java程序设计基础》异常的处理的全部内容,更多相关《java程序设计基础》异常内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(104)

评论列表共有 0 条评论

立即
投稿
返回
顶部