常见:
算术异常类:ArithmeticExecption
空指针异常类:NullPointerException
类型强制转换异常:ClassCastException
数组负下标异常:NegativeArrayException
数组下标越界异常:ArrayIndexOutOfBoundsException
文件未找到异常:FileNotFoundException
方法未找到异常:NoSuchMethodException
自定义异常处理:
Exception1.java public class Exception1 extends Exception{ private int num; public Exception1(int a) { this.num=a; } public String toString(){ return num+"的值必须大于10!"; } }
Exception2.java public class Exception2 extends Exception{ private int num; public Exception2(int a) { this.num=a; } public String toString(){ return num+"的值必须小于100!"; } }
Test.java public class Test { public void makeException(int x) throws Exception1,Exception2 { if(x<10) { throw new Exception1(x); } else if(x>100) { throw new Exception2(x); } else System.out.println("ok"); } public static void main(String args[]) { Test t=new Test(); try{ t.makeException(118); } catch(Exception1 e) { System.out.println(e); } catch(Exception2 e) { System.out.println(e); } } }