总体上我们根据Javac对异常的处理要求,将异常类分为2类。
非检查异常(unckecked exception):Error 和 RuntimeException 以及他们的子类。javac在编译时,不会提示和发现这样的异常,不要求在程序处理这些异常。所以如果愿意,我们可以编写代码处理(使用try…catch…finally)这样的异常,也可以不处理。对于这些异常,我们应该修正代码,而不是去通过异常处理器处理 。这样的异常发生的原因多半是代码写的有问题。如除0错误ArithmeticException,错误的强制类型转换错误ClassCastException,数组索引越界ArrayIndexOutOfBoundsException,使用了空对象NullPointerException等等。
检查异常(checked exception):除了Error 和 RuntimeException的其它异常。javac强制要求程序员为这样的异常做预备处理工作(使用try…catch…finally或者throws)。在方法中要么用try-catch语句捕获它并处理,要么用throws子句声明抛出它,否则编译不会通过。这样的异常一般是由程序的运行环境导致的。因为程序可能被运行在各种未知的环境下,而程序员无法干预用户如何使用他编写的程序,于是程序员就应该为这样的异常时刻准备着。如SQLException , IOException,ClassNotFoundException 等。
需要明确的是:检查和非检查是对于javac来说的,这样就很好理解和区分了。
/** * MIT License * Copyright (c) 2018 haihua.liu * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package cn.liuhaihua.web.exception; /** * @ClassName: ServiceException * @Description: 自定义service异常 * @author Liuhaihua * @date 2018年7月5日 * */ public class ServiceException extends RuntimeException { private static final long serialVersionUID = 1L; /** * 错误编码 */ private String errorCode; /** * 消息是否为属性文件中的Key */ private boolean propertiesKey = true; /** * 构造一个基本异常. * * @param message * 信息描述 */ public ServiceException(String message) { super(message); } /** * 构造一个基本异常. * * @param errorCode * 错误编码 * @param message * 信息描述 */ public ServiceException(String errorCode, String message) { this(errorCode, message, true); } /** * 构造一个基本异常. * * @param errorCode * 错误编码 * @param message * 信息描述 */ public ServiceException(String errorCode, String message, Throwable cause) { this(errorCode, message, cause, true); } /** * 构造一个基本异常. * * @param errorCode * 错误编码 * @param message * 信息描述 * @param propertiesKey * 消息是否为属性文件中的Key */ public ServiceException(String errorCode, String message, boolean propertiesKey) { super(message); this.setErrorCode(errorCode); this.setPropertiesKey(propertiesKey); } /** * 构造一个基本异常. * * @param errorCode * 错误编码 * @param message * 信息描述 */ public ServiceException(String errorCode, String message, Throwable cause, boolean propertiesKey) { super(message, cause); this.setErrorCode(errorCode); this.setPropertiesKey(propertiesKey); } /** * 构造一个基本异常. * * @param message * 信息描述 * @param cause * 根异常类(可以存入任何异常) */ public ServiceException(String message, Throwable cause) { super(message, cause); } public String getErrorCode() { return errorCode; } public void setErrorCode(String errorCode) { this.errorCode = errorCode; } public boolean isPropertiesKey() { return propertiesKey; } public void setPropertiesKey(boolean propertiesKey) { this.propertiesKey = propertiesKey; } }
当使用多个catch语句块来捕获异常时,需要将父类的catch语句块放到子类型的catch块之后,这样才能保证后续的catch可能被执行,否则子类型的catch将永远无法到达,Java编译器会报编译错误。
如果try语句块中存在return语句,那么首先会执行finally语句块中的代码,然后才返回。
如果try语句块中存在System.exit(0)语句,那么久不会执行finally语句块的代码了,因为System.exit(0)会终止当前运行的JVM。程序在JVM终止前结束执行。