本文概览:介绍了异常种类、异常堆栈和自定义异常。
1 异常种类
异常层次结构为:
对于Erro类【未检查异常】,描述的是系统内部错误类和资源耗尽错误。在代码中不抛出这种类型的对象,只需要关注Exception层次结构。
RuntimeExcetion【未检查异常】,就是运行时刻异常。我们在代码中自定义的异常都属于该类型。
IOException【已检查异常】。Throws只声明检查类型异常,即IOException类型;对于RuntimeException类型不声明。
2 堆栈
1、 堆栈信息理解
打印最上面的就是 最底层抛出异常的地方。举例如
main(){
f1();
}
f1(){
f2();
}
f2(){
f3()
}
1
2
3
4
5
6
7
8
9
main(){
f1();
}
f1(){
f2();
}
f2(){
f3()
}
如果执行main()时f3()有异常,则此时打印堆栈信息如下:
f3()异常
f2()位置
f1()位置
main()位置
2、堆栈信息的传递
public class TestExcetpion {
private void f1() {
throw new RuntimeException("f1");
}
private void f2() {
try {
f1();
} catch (Exception e) {
throw new RuntimeException("f2");
}
}
public void f3() {
try{
f2();
}catch (Exception e){
throw new RuntimeException("f3",e);}
}
public static void main(String[] args){
TestExcetpion e = new TestExcetpion();
e.f3();
}
}
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
publicclassTestExcetpion{
privatevoidf1(){
thrownewRuntimeException("f1");
}
privatevoidf2(){
try{
f1();
}catch(Exceptione){
thrownewRuntimeException("f2");
}
}
publicvoidf3(){
try{
f2();
}catch(Exceptione){
thrownewRuntimeException("f3",e);}
}
publicstaticvoidmain(String[]args){
TestExcetpione=newTestExcetpion();
e.f3();
}
}
如下f3的堆栈信息只能到f2,不能延续到f1了。
Exception in thread “main” java.lang.RuntimeException: f3
at exception.TestExcetpion.f3(TestExcetpion.java:25)
at exception.TestExcetpion.main(TestExcetpion.java:31)
……..
Caused by: java.lang.RuntimeException: f2
at exception.TestExcetpion.f2(TestExcetpion.java:17)
at exception.TestExcetpion.f3(TestExcetpion.java:23)
… 6 more
(2)如果修改f3
public void f2() {
try{
f1();
}catch (Exception e){
throw new RuntimeException("f2",e);}
}
1
2
3
4
5
6
publicvoidf2(){
try{
f1();
}catch(Exceptione){
thrownewRuntimeException("f2",e);}
}
f3的堆栈信息才能向下延续到f1`
Exception in thread “main” java.lang.RuntimeException: f3
at exception.TestExcetpion.f3(TestExcetpion.java:23)
at exception.TestExcetpion.main(TestExcetpion.java:29)
……
Caused by: java.lang.RuntimeException: f2
at exception.TestExcetpion.f2(TestExcetpion.java:15)
at exception.TestExcetpion.f3(TestExcetpion.java:21)
… 6 more
Caused by: java.lang.RuntimeException: f1
at exception.TestExcetpion.f1(TestExcetpion.java:9)
at exception.TestExcetpion.f2(TestExcetpion.java:13)
… 7 more
3 自定义异常
1、自定义异常作用
是为了对于每一种异常都设置一个错误码。
2、自定义异常
public class MonitorException extends RuntimeException {
private String errorCode;
private String errorMsg;
private MonitorErrorEnum errorEnum;
public MonitorException() {
super();
}
public MonitorException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public MonitorErrorEnum getErrorEnum() {
return errorEnum;
}
public void setErrorEnum(MonitorErrorEnum errorEnum) {
this.errorEnum = errorEnum;
}
}
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
publicclassMonitorExceptionextendsRuntimeException{
privateStringerrorCode;
privateStringerrorMsg;
privateMonitorErrorEnumerrorEnum;
publicMonitorException(){
super();
}
publicMonitorException(StringerrorCode,StringerrorMsg){
super(errorCode);
this.errorCode=errorCode;
this.errorMsg=errorMsg;
}
publicStringgetErrorCode(){
returnerrorCode;
}
publicvoidsetErrorCode(StringerrorCode){
this.errorCode=errorCode;
}
publicStringgetErrorMsg(){
returnerrorMsg;
}
publicvoidsetErrorMsg(StringerrorMsg){
this.errorMsg=errorMsg;
}
publicMonitorErrorEnumgetErrorEnum(){
returnerrorEnum;
}
publicvoidsetErrorEnum(MonitorErrorEnumerrorEnum){
this.errorEnum=errorEnum;
}
}
3、自定义错误码
public enum MonitorErrorEnum {
SUCESS("0", "成功"),
FAIL("9999", "失败");
private String errCode;
private String errMessage;
MonitorErrorEnum(String errCode, String errMessage) {
this.errCode = errCode;
this.errMessage = errMessage;
}
public String getErrCode() {
return errCode;
}
public void setErrCode(String errCode) {
this.errCode = errCode;
}
public String getErrMessage() {
return errMessage;
}
public void setErrMessage(String errMessage) {
this.errMessage = errMessage;
}
}
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
publicenumMonitorErrorEnum{
SUCESS("0","成功"),
FAIL("9999","失败");
privateStringerrCode;
privateStringerrMessage;
MonitorErrorEnum(StringerrCode,StringerrMessage){
this.errCode=errCode;
this.errMessage=errMessage;
}
publicStringgetErrCode(){
returnerrCode;
}
publicvoidsetErrCode(StringerrCode){
this.errCode=errCode;
}
publicStringgetErrMessage(){
returnerrMessage;
}
publicvoidsetErrMessage(StringerrMessage){
this.errMessage=errMessage;
}
}
(全文完)