Java中常见异常举例

1.常见异常类型
 java.lang.RuntimeException
ClassCastException
ArrayIndexOutOfBoundsException
NullPointerException
ArithmeticException
NumberFormatException
InputMismatchException
。。。
 java.io.IOExeption
 FileNotFoundException
 EOFException
 java.lang.ClassNotFoundException
 java.lang.InterruptedException
 java.io.FileNotFoundException
 java.sql.SQLException

2.常见异常代码举例
ArrayIndexOutOfBoundsException

public class IndexOutExp {
	public static void main(String[] args) {
		String friends[] = { "lisa", "bily", "kessy" };
		for (int i = 0; i < 5; i++) {
			System.out.println(friends[i]); // friends[4]?
		}
	System.out.println("\nthis is the end");
	}
}

Java中常见异常举例_常见异常
NullPointerException

public class NullRef {
	int i = 1;
	public static void main(String[] args) {
		NullRef t = new NullRef();
		t = null;
		System.out.println(t.i);
	}
}

Java中常见异常举例_常见异常_02
ArithmeticException

public class DivideZero {
	int x;
	public static void main(String[] args) {
		int y;
		DivideZero c=new DivideZero();
		y=3/c.x;
		System.out.println("program ends ok!");
	}
}

Java中常见异常举例_异常类型_03
ClassCastException

public class Order {
	public static void main(String[] args) {
		Object obj = new Date();
		Order order;
		order = (Order) obj;
		System.out.println(order);
	}
}

Java中常见异常举例_异常类型_04