java基础面试题:运行时异常与一般异常有何异同?error和exception有什么区别? 请写出你最常见到的5个runtimeexception?

Throwable是Java错误处理的父类,有两个子类:Error和Exception。

Error:无法预期的严重错误,导致JVM虚拟机无法继续执行,几乎无法恢复捕捉的

Exception:可恢复捕捉的。java健壮程序的手段。

Java提供了两类主要的异常:runtime exception和checked exception (编译时被检查的异常)。

checked exception (编译时被检查的异常):JAVA编译器强制要求我们必需对出现的这些异常进行catch或throws。所以,面对这种异常不管我们是否愿意,只能写一大堆catch块或throws去处理可能的异常。

如IO异常,以及SQL异常等。

runtime exception:编译通过,但运行通不过,出现RuntimeException,通常是程序员出错。虚拟机接管会终止线程或主程序。如错误的类型转换、数组越界访问和访问空指针等

最常见到的runtime exception

1、NullPointerException:

int a1[]=null;
System.out.print(a1[2]);

2、ArrayIndexOutOfBoundsException

int a[]={2,3,5,32,6};
for (int i = 0; i <6; i++)
{
System.out.print(a[i]);
}

3、ClassCastException

Object i=new Integer(1);
System.out.println((String)i);

4、ArithmeticException

int a=5/0;

5、NegativeArraySizeException

String[] s=new String[-10];

原文地址:https://www.cnblogs.com/qingyundian/p/8336883.html