1. Throwable的子类包含哪两类?简述Java Error类与Exception类的区别。
Error: 致命异常。在正常情况下,不大可能出现的情况。标识系统发生了不可控的错误。程序无法处理,只能人工介入。例如, 虚拟机产生的错误StackOverflowError、OutOfMemoryError。
Exception: 非致命异常。程序可处理。分为受编译器检测的checked异常(受检异常)和不受编译器检测的unchecked异常(非受检异常)。
2. Exception又分为checked异常和unchecked异常,请分别举例说明。
- Checked exception: 继承自 Exception 类。代码需要处理 API 抛出的 checked exception,要么用 catch 语句,要么直接用 throws 语句抛出去。可以理解为编译时异常。例如IOException
- Unchecked exception: 也称 RuntimeException,它也是继承自 Exception。但所有 RuntimeException 的子类都有个特点,就是代码不需要处理它们的异常也能通过编译,所以它们称作 unchecked exception。RuntimeException(运行时异常)不需要try...catch...或throws 机制去处理的异常。运行时异常。
将派生于Error或者RuntimeException的异常称为unchecked异常,所有其他的异常成为checked异常。
3. 请查阅资料,简述StackOverflowError和OutOfMemoryError两类错误的发生情形和原因。
StackOverflowError:
>>发生的地方:虚拟机栈和本地方法栈
>>线程调用生成的栈桢深度超过了虚拟机允许的深度(比如一个方法A内调用了方法B,然后B又调用C...这样调用了很多很多层,但是虚拟机只能提供一定数量层),不过现在大多数虚拟机栈的调动深度都是可以动态扩展的,所以这个问题出现的概率非常的低。
OutOfMemoryError
>>发生的地方:基本上虚拟机的运行时数据区域(堆,栈,方法区)都会发生
>>原因:内存不够,发生了内存溢出
>>处理:申请更多内存,调整虚拟机的启动参数
4. 简述异常处理的两种方式,并举例说明区别。
显示声明抛出:
例如
隐式声明抛出:
例如:
class Pare2 {
//NullPointerException示例(main方法签名后可省去throws声明)
private static int[] x;
public static void main(String[] args){
System.out.println(x[0]);
}
/*使用前先进行判空
if(x == null) {
//做相应处理
}*/
}
程序捕获处理:
try {
statements
}
catch (ExceptionType1 ExceptionObject){
Exception Handling
}
catch (ExceptionType2 ExceptionObject) {
Exception Handling
}……
finally{
Finally Handling
}
注意:finally是这个组合语句的统一出口,一般用来进行一些善后清理操作,例如清理资源、释放连接、关闭文件、管道流等操作。它是可选的部分,但一旦选定,必定执行。
5. 选取RuntimeException类的五个子类,编写抛出并捕获上述子类异常的程序。(例如算术异常,空指针异常,类转换异常,数组越界异常等)
空指针异常:
public class TestShape {
public static void main(String[] args) {
int[] arr1=null;
try{
System.out.println(arr1[0]);
}
catch(NullPointerException e){
System.out.println("发生NullPointerException");
}
}
}//发生NullPointerException
数组越界异常:
public class TestShape {
public static void main(String[] args) {
int[] arr1=new int[5];
try{
System.out.println(arr1[7]);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("发生ArrayIndexOutOfBoundsException");
}
}
}//发生ArrayIndexOutOfBoundsException
算术异常:
public class TestShape {
public static void main(String[] args) {
try{
System.out.println(3/0);
}
catch(ArithmeticException e){
System.out.println("发生ArithmeticException");
}
}
}//发生ArithmeticException
类转换异常:
//将一个数字强制转换成字符串就会报这个异常
public class TestShape {
public static void main(String[] args) {
try{
Object x = new Integer(0);
System.out.println((String)x);
}
catch(ClassCastException e){
System.out.println("发生ClassCastException");
}
}
}//发生ClassCastException
数组负下标异常:
public class TestShape {
public static void main(String[] args) {
try{
int [] arr1=new int[-1];
}
catch(NegativeArraySizeException e){
System.out.println("发生NegativeArraySizeException");
}
}
}//发生NegativeArraySizeException
6. 根据某业务场景自定义一个异常类,并在某场景下抛出该异常对象。
public class TestShape {
public static void function() throws NumberFormatException{
String s = "abc";
System.out.println(Double.parseDouble(s));
}
public static void main(String[] args) {
try {
function();
} catch (NumberFormatException e) {
System.err.println("非数据类型不能转换。");
//e.printStackTrace();
}
}
}
7. 异常中的throws声明与throw语句的区别是什么?请举例说明。
>>throw:就是自己进行异常处理,throw一旦进入被执行,程序立即会转入异常处理阶段,后面的语句就不再执行, 而且所在的方法不再返回有意义的值!
>>throws:用来声明一个方法可能抛出的所有异常信息,将异常声明但是不处理,而是将异常往上传,谁调用我就 交给谁处理。
区别:
>>throws出现在方法函数头,表示该方法可能会抛出的异常,允许throws后面跟着多个异常类型;而throw出现在函数体,throw抛出一个异常对象,且只能有一个,当方法在执行过程中遇到异常情况时,将异常信息封装为异常对象,然后throw。
>>throws表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某种异常对象。
public class TestShape {
public static void main(String[] args) {
String s = "abc";
if(s.equals("abc")) {
throw new NumberFormatException();
} else {
System.out.println(s);
}
}
}
public class TestShape {
public static void function() throws NumberFormatException{
String s = "abc";
System.out.println(Double.parseDouble(s));
}
public static void main(String[] args) {
try {
function();
} catch (NumberFormatException e) {
System.err.println("非数据类型不能转换。");
//e.printStackTrace();
}
}
}
8. finally子句的作用是什么?
finally是这个组合语句的统一出口,一般用来进行一些善后清理操作,例如清理资源、释放连接、关闭文件、管道流等操作。它是可选的部分,但一旦选定,必定执行。
public class TestShape {
public static void main(String[] args) {
System.out.println(testFinally());
}
private static int testFinally() {
int temp = 100;
try {
System.out.println("Try");
return ++temp;// return的结果被暂存
}
finally {
temp = 1;
System.out.println(temp);
System.out.println( "Finally" );
}
}
}