5.13异常类

/**
 * 异常:
 * 		编译时期异常:只要出现的不是运行时期异常,统称为编译时期  日期的文本格式---解析   java.util.Date类型:ParseException:解析异常
 * 					编译时期异常:开发者必须处理!
 * 		运行时期异常:RuntimeException
 * 					编译通过了,但是开发者代码不严谨(NullPointerExceptino等等...)
 * 异常的处理分为两种:
 *		1)try...catch...finally (标准格式) :捕获异常,finally中的代码必然会执行
 *		2)throws ...		抛出异常
 * */
public class Text1 {
	public static void main(String[] args) {
		int a = 10;
		int b = 0;
		int[] array = {1,2,3};
		try {
			a = a/b;
			b = array[3];
		}catch(ArithmeticException e) {
			b = 1;
			System.out.println("O不能作为除数");
		}catch(ArrayIndexOutOfBoundsException e) {//当开发者解决前一个异常以后才会才会catch下一个异常
			System.out.println("数组角标越界");
		}finally {
			System.out.println("hello");
		}
		System.out.println(a);
	}
}
package org.westos.异常类博客练习;
//异常类:
//		try...catch...简化格式
public class Text2 {
	public static void main(String[] args) {
		int a = 10;
		int b = 0;
		int[] array = {1,2,3};
		try {
			a = a/b;
			b = array[3];
		}catch(ArithmeticException|ArrayIndexOutOfBoundsException e) {
			System.out.println("程序出问题了");
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值