异常的关系:
Throwable
Error Exception
Runtime Exception
异常的分类:
Error: 称为错误,由Java虚拟机生成并抛出,包括动态链接失败,虚拟机错误等,程序对其不做处理
Exception:所有异常类的父类,其子类对应了各种各样可能出现的异常事件,一般需要用户显示的声明或捕获
Runtime Exception:一类特殊的异常,如:被0除,数组下标超范围等,其产生比较频繁,处理麻烦,如果用户显示的声明或捕获将会对程序的可读性
和运行效率影响很大,因此由系统自动检测并将它们交给缺省的异常处理程序(用户可不必对其处理)
如果某个方法后面跟着throws 抛出异常的类型 那么必须try catch
关键字:
throws 要抛出异常的类型
throw 抛出异常对象
基本流程:
try{
try语句
}catch(异常类型1 异常对象1){ //捕捉异常 如果有异常对象与异常类型匹配 执行异常处理语句
异常处理语句
}catch(异常类型2 异常对象2){ //如果没有异常发生或没有异常对象与异常类型匹配 那么就跳过异常处理语句
异常处理语句
}finally{ //无论是否捕捉异常 最终都会执行finally语句 一般finally语句用于关闭资源
finally语句
}
小知识点:
注意:子类重写父类的方法必须抛出与原方法所抛出异常的类型一致,或不抛出异常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 |
package
Test14; import
java.io.IOException; //父类 public
class A { public
void call() throws
IOException{ System.out.println( "bb" ); } } package
Test14; import
java.io.FileNotFoundException; //子类 public
class B1 extends
A { public
void call() throws
ArithmeticExecption{ //与父类方法抛出的异常不一致,错误!! System.out.println( "aa" ); } public
static void main(String[] args) { try
{ new
B1().call(); } catch
(FileNotFoundException e) { e.printStackTrace(); } } } package
Test14; import
java.io.IOException; //子类 public
class B2 extends
A { public
void call() throws
EOFException { //与父类方法抛出的异常不一致,错误!! System.out.println( "cc" ); } public
static void main(String[] args) { new
B2().call(); } } |
例子1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 |
//加法类 public
class Add { public
int add(Object number) throws
ArithmeticException{ //throws关键字后边跟的是要抛出异常的类型 if (number.equals( "a" )){ throw
new ArithmeticException( "传进来的值不合法" ); //throw关键字后边才是抛出的异常对象 也可以不抛出这个异常对象,直接throws 这时调用这个方法的方法也可以catch(捕获) } return
0 ; } } //测试类 public
class Test { public
static void main(String[] args) { try { Add add= new
Add(); add.add( "a" ); //add方法会抛出异常 } catch (ArithmeticException e){ //捕获add方法抛出的异常对象并处理 System.out.println( "传进去的值不能是字符串" ); } } } 例子 2 : package
Test14; //定义自己的异常 public
class MyException extends
Exception { private
int id; // 异常的id号 public
MyException(String message, int
id) { super (message); this .id = id; } public
int getId() { return
id; } } package
Test14; public
class Test { //判断登记人数 public
void regist( int
num) throws
MyException{ if (num< 0 ){ throw
new MyException( "人数为负值,不合法" , 3 ); //3为这个异常的id } System.out.println( "登记人数" +num); } //调用管理方法 public
void manager(){ try
{ //调用登记人数 regist(- 1 ); } catch
(MyException e) { System.out.println( "登记失败,出错代码:" +e.getId()); e.printStackTrace(); } System.out.println( "操作结束" ); } public
static void main(String[] args) { new
Test().manager(); } } |
异常信息:
登记失败,出错代码:3
操作结束
Test14.MyException: 人数为负值,不合法
at Test14.Test.regist(Test.java:7)
at Test14.Test.manager(Test.java:15)
at Test14.Test.main(Test.java:23)
原文:http://www.cnblogs.com/gdds/p/3641503.html