/** * 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终止前结束执行。