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");
}
}
NullPointerException
public class NullRef {
int i = 1;
public static void main(String[] args) {
NullRef t = new NullRef();
t = null;
System.out.println(t.i);
}
}
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!");
}
}
ClassCastException
public class Order {
public static void main(String[] args) {
Object obj = new Date();
Order order;
order = (Order) obj;
System.out.println(order);
}
}