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("程序出问题了");
		}
	}
}


猜你喜欢

转载自blog.csdn.net/ws1995_java/article/details/80349195