先来一块儿看看下面的代码:java
package com.huangzx.Exception;
/** * @author huangzx * @date 2018/11/27 */
public class ExceptionTypeTest {
public class ExceptionTypeTest {
public int doSomething() throws ArithmeticException {
return 5 / 0;
}
public static void main(String[] args) {
ExceptionTypeTest ett = new ExceptionTypeTest();
ett.doSomething();
}
}
复制代码
问:上面的代码能编译经过吗?程序员
答:能够。ArithmeticException是RuntimeException异常,能够不进行捕获或抛出。 若是改成IOException,以下:测试
package com.huangzx.Exception;
import java.io.*;
/** * @author huangzx * @date 2018/11/27 */
public class ExceptionTypeTest {
public void doSomething() throws IOException {
InputStream is = new FileInputStream(new File("/Users/huangzx/test.txt"));
is.read();
}
public static void main(String[] args) {
ExceptionTypeTest ett = new ExceptionTypeTest();
ett.doSomething();
}
}
复制代码
问:它还能编译经过吗?优化
答:不能。IOException是直接继承自Exception的异常,它必须获得处理,要么捕获要么抛出。this
上面两个例子中,ArithmeticException与IOException都是来自Exception体系,为何一个不须要处理,一个须要处理呢?这就涉及到两个概念:受检异常和非受检异常。 先来复习一下Java异常体系:spa
二者的表明人物出场(掌声有请~~):code
既然二者的概念清晰了,处理方式也随之昭然若揭。cdn
针对受检异常:blog
针对非受检异常:继承
以上是打基础,下面玩点高级的。
当须要一些跟特定业务相关的异常信息类时,咱们能够在Java自身定义的异常以外,编写继承自Exception的受检异常类,也能够编写继承自RuntimeException或其子类的非受检异常类。
通常状况下,异常类提供了默认构造器和一个带有String类型参数的构造器。咱们的自定义异常,只须要实现这两个构造器就足够用了。由于最有价值的是咱们定义的异常类型(即异常的名字),当异常发生时,咱们只要看到了这个名字就知道发生了什么。因此除非有一些特殊操做,不然自定义异常只需简单实现构造器便可。
示例:
一、自定义业务异常类,继承自RuntimeException。
package com.huangzx.Exception;
/** * @author huangzx * @date 2018/11/27 */
public class BusinessException extends RuntimeException {
private String code; // 异常返回码
private String msg; // 异常信息
public BusinessException() {
super();
}
public BusinessException(String message) {
super(message);
this.code = code;
}
public BusinessException(String code, String msg) {
super();
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
复制代码
二、定义方法,声明throws这个自定义异常。要使用它,必须通知调用代码的类,作好“抱接”这个异常的心理准备。代码逻辑中异常发生的点,须要throw抛出。
public void logicCode() throws BusinessException {
throw new BusinessException("-1000", "业务出错");
}
复制代码
三、测试自定义异常main方法
public static void main(String[] args) {
ExceptionTest et = new ExceptionTest();
try {
et.logicCode();
} catch (BusinessException e) {
e.printStackTrace();
System.out.println("code=" + e.getCode() + "msg=" + e.getMsg());
}
}
复制代码
基本上,就差很少了。另外还有一个须要注意的点,就是:千万不要在finally再抛出一个很是规异常,由于它一定会执行,致使你try{}监测的代码块捕获的异常得不处处理,会起到混淆视听的效果,让你排查问题时摸不着头脑,请切记。示例以下:
package com.huangzx.Exception;
/** * @author huangzx * @date 2018/11/27 */
public class ExceptionLoseTest {
public static int throwException() throws Exception {
try {
throw new Exception();
} catch (Exception e) {
throw e;
} finally {
throw new NullPointerException();
}
}
public static void main(String[] args) {
try {
throwException();
} catch (Exception e) {
e.printStackTrace();
}
}
}
复制代码
执行结果以下:
java.lang.NullPointerException
at com.huangzx.Exception.ExceptionLoseTest.throwException(ExceptionLoseTest.java:14)
at com.huangzx.Exception.ExceptionLoseTest.main(ExceptionLoseTest.java:20)
复制代码
好了,下课。。。(老师~再见~~)