RuntimeException
RuntimeException是非检查型异常,所有方法都可以在不声明throws的情况下抛出RuntimeException及其子类。
public class TestA { public static void main(String[] args) { throw new RuntimeException(); } }
上面代码的编译没有问题。
常见的RuntimeException有:
NullPointerException:空指针异常 NumberFormatException:继承IllegalArgumentException,字符串转换为数字时异常。 比如int i= Integer.parseInt("ab3"); ArrayIndexOutOfBoundsException:数组越界 比如 int[] a=new int[3]; int b=a[3]; StringIndexOutOfBoundsException:字符串越界 比如 String s="hello"; char c=s.chatAt(6); ClassCastException:类型转换错误 比如 Object obj=new Object(); String s=(String)obj; ClassCastException:类型转换错误 比如 Object obj=new Object(); String s=(String)obj; UnsupportedOperationException:该***作不被支持,如果我们希望不支持这个方法,可以抛出这个异常。既然不支持还要这个干吗?有可能子类中不想支持父类中有的方法,可以直接抛出这个异常。 ArithmeticException:算术错误,典型的就是0作为除数的时候。 IllegalArgumentException:非法参数,在把字符串转换成数字的时候经常出现的一个异常,我们可以在自己的程序中好好利用这个异常。