1. Throwable的子类包含哪两类?简述Java Error类与Exception类的区别。
Throwable子类:
Error:
致命 异常,标识系统发生了不可控的错误。程序无法处理,只能人工介入。例如,虚拟机产生了StackOverflowError,OutOfMemoryError。
Exception:
非致命 异常。程序可处理。分为受编辑器检测的checked异常(受检异常)和不受编辑器检测的unchecked异常(非受检异常)。
2. Exception又分为checked异常和unchecked异常,请分别举例说明。
1.Exception分类
如果需要弄明白Exception类中的checked异常和unchecked异常,我们首先需要了解Exception分类,Exception分类具体如下图所示:
2.checked异常和unchecked异常的定义
派生于Error或者RuntimeException的异常称为unchecked异常,所有其他的异常成为checked异常。
3.举例分析:
unchecked异常
NullPointerException示例(main方法签名后可省去throws声明)
public class TestArray {
private static int[] x;
public static void main(String[] args){
System.out.println(x[0]);
}
}
//该程序会出现一下提示信息:
//Exception in thread "main" java.lang.NullPointerException
//at TestArray.main(TestArray.java:4)
常见的unchecked异常:
- NullPointerException
- ArrayIndexOutOfBoundsException
- ArithmeticException
- IllegalArgumentException
3. 请查阅资料,简述StackOverflowError和OutOfMemoryError两类错误的发生情形和原因。
StackOverflowError (栈溢出):
- 发生原因:如果程序使用的栈深度超过虚拟机分配给线程的栈大小时,抛出该错误。
OutOfMemoryError (内存溢出):
- 发生原因:如果程序在对应栈中无法申请到足够的内存空间,则抛出该错误。
栈溢出举例:
class A{
public static void main(String[]args){
main(null);
}
}
内存溢出举例:
class A{
public static void main(String[]args){
int []a =new int[999999999];
}
}
4. 简述异常处理的两种方式,并举例说明区别。
隐式声明处理
1 import java.util.*;
2 class TestEmptyStack {
3 public static void main(String[] args){
4 Stack st = new Stack();
5 Object ob = st.pop();
6 }
7
8 /*
9 Exception in thread "main" java.util.EmptyStackException
10 at java.util.Stack.peek(Unknown Source)
11 at java.util.Stack.pop(Unknown Source)
12 at TestEmptyStack.main(TestEmptyStack.java:5)
13 */
显示声明处理
1 import java.io. *;
2 class TestScreenIn{
3 public static void main(String[] args) throws IOException{ //抛出IOException异常
4 BufferedReader keyin = new BufferReader(new
5 InputStreamReader(System.in));
6 String c1;
7 int i=0;
8 String[] e = new String[10];
9 while( i<10){
10 c1 = keyin.readLine();
11 e[i] = c1;
12 i++;
13 }
14 }
15 }
异常捕获处理
1 public static void main(String[] args){
2
3 try{
4 .
5 .
6 .
7 c1 = keyin.readLine();
8 .
9 .
10 .
11 }
12 catch(IOException e){
13 //e.printStackTrace();
14 System.out.println("系统IO有错误");
15 }
16 }
5. 选取RuntimeException类的五个子类,编写抛出并捕获上述子类异常的程序。(例如算术异常,空指针异常,类转换异常,数组越界异常等)
1 package TestException;
2
3 class Father{
4 int i;
5 }
6
7 class Son extends Father{
8 public Son(int i){
9 this.i=i;
10 }
11 public int getI(){
12 return this.i;
13 }
14 }
15
16 public class CatchException {
17 public static void main(String[] args) {
18 int a=7;
19 int b=0;
20 int c;
21 Father father1 = new Father();
22 Son son1 ;
23 Son son2 = null;
24 int[] x = new int[1];
25
26 try{
27 c=a/b;
28 }
29
30 catch (ArithmeticException e){
31 e.printStackTrace();
32 System.out.println("这里发生了算术异常了!!!");
33 }
34 try{
35 son1= (Son) father1;
36 }
37
38 catch (ClassCastException e){
39 e.printStackTrace();
40 System.out.println("这里发生了类转换异常!!!");
41 }
42
43 try{
44 son2.getI();
45 }
46
47 catch (NullPointerException e){
48 e.printStackTrace();
49 System.out.println("这里发生了空指针异常!!!");
50 }
51
52 try{
53 System.out.println(x[2]);
54 }
55
56 catch (ArrayIndexOutOfBoundsException e){
57 e.printStackTrace();
58 System.out.println("这里发生了数组越界异常!!!");
59 }
60
61 }
62 }
6. 根据某业务场景自定义一个异常类,并在某场景下抛出该异常对象。
非法玩家检测
class Player {
private int HP = 2147483647;
int getHP() {
return HP;
}
}
class MoudlePlayer {
private final int MinHP = 1;
private final int MaxHP = 1000;
int getMinHP() {
return MinHP;
}
int getMaxHP() {
return MaxHP;
}
}
class PlayerAttributeValueException extends Exception {
private static MoudlePlayer moudlePlayer = new MoudlePlayer();
//调用Exception的构造方法
PlayerAttributeValueException(String msg) {
super(msg);
}
//仅声明异常,向上抛出异常
static void throwError(Player player) throws PlayerAttributeValueException {
int testHP = player.getHP();
if (testHP < moudlePlayer.getMinHP() || testHP > moudlePlayer.getMaxHP()) {
throw new PlayerAttributeValueException("玩家属性数值错误,即将封号!!!");
}
}
}
class Manager {
public static void main(String[] args) {
try {
Player moota = new Player();
PlayerAttributeValueException.throwError(moota);
} catch (PlayerAttributeValueException e) {
e.printStackTrace();
}
}
}
7. 异常中的throws声明与throw语句的区别是什么?请举例说明。
-
throw:
表示方法内抛出某种异常对象
-
throws:
方法的定义上使用 throws 表示这个方法可能抛出某种异常
8. finally子句的作用是什么?
- 和 try-catch 捕获异常连用,用于捕获异常的结束工作。
- 正常情况一定会执行。如果虚拟机停止或出现问题或者程序存在死循环则不执行。