Java异常类 博客分类: JavaException Exception类封装
程序员文章站
2024-03-16 12:20:46
...
ApplicationException
/** * Wallstreet异常 * * @author liuxl * */ public class ApplicationException extends RuntimeException { /** * */ private static final long serialVersionUID = 1L; /** * 错误代码 */ private String code; public ApplicationException() { super(); } public ApplicationException(String msg) { super(msg); } public ApplicationException(Throwable th) { super(th); } public ApplicationException(String msg, Throwable th) { super(msg, th); } public ApplicationException(String code, String msg) { super(msg); this.code = code; } public ApplicationException(String code, String msg, Throwable th) { super(msg, th); this.code = code; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
InvalidParamException
/** * 输入参数校验异常 * * @author liuxl * */ public class InvalidParamException extends ApplicationException { /** * */ private static final long serialVersionUID = 1L; // 错误参数名称 private String paramName; public InvalidParamException(String paramName) { super("Invalid param: " + paramName); this.setParamName(paramName); } public InvalidParamException(String msg, Throwable th) { super(msg, th); } public InvalidParamException(String code, String msg) { super(code, msg); } public String getParamName() { return paramName; } public void setParamName(String paramName) { this.paramName = paramName; } }
EmptyResultException
/** * 没有查询到记录的异常 * * @author liuxl * */ public class EmptyResultException extends ApplicationException { /** * */ private static final long serialVersionUID = 1L; public EmptyResultException(String msg) { super(msg); } public EmptyResultException(Throwable th) { super(th); } public EmptyResultException(String msg, Throwable th) { super(msg, th); } public EmptyResultException(String code, String msg) { super(code, msg); } public EmptyResultException(String code, String msg, Throwable th) { super(code, msg, th); } }
Exceptions
import java.io.PrintWriter; import java.io.StringWriter; /** * 关于异常的工具类. * * @author admin */ public class Exceptions { /** * 将CheckedException转换为UncheckedException. */ public static RuntimeException unchecked(Exception e) { if (e instanceof RuntimeException) { return (RuntimeException) e; } else { return new RuntimeException(e); } } /** * 将ErrorStack转化为String. */ public static String getStackTraceAsString(Exception e) { StringWriter stringWriter = new StringWriter(); e.printStackTrace(new PrintWriter(stringWriter)); return stringWriter.toString(); } /** * 判断异常是否由某些底层的异常引起. */ public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses) { Throwable cause = ex.getCause(); while (cause != null) { for (Class<? extends Exception> causeClass : causeExceptionClasses) { if (causeClass.isInstance(cause)) { return true; } } cause = cause.getCause(); } return false; } }
上一篇: 选择城市组件(多选)
下一篇: 六大排序算法的实现
推荐阅读
-
SerializeUtil工具类 博客分类: Java SerializeUtil
-
Map 对象与 JavaBean 对象互转工具类 博客分类: Java BeanToMapUtilMap 对象与 JavaBean 对象互转工具类
-
Java异常类 博客分类: JavaException Exception类封装
-
将类文件打包生成jar文件 博客分类: java
-
将类文件打包生成jar文件 博客分类: java
-
DateTime 类常用API 博客分类: java DateTimeDate日期时区
-
一个操作时间的类 博客分类: java CC++C#SQL
-
java对象的存储 博客分类: java基础类 java数据存储
-
java在cmd下编译和执行引用jar的类 博客分类: Java java编译和执行引用第三方jarcmd
-
JAVA: 为什么要使用"抽象类"? 使用"抽象类"有什么好处? 博客分类: JAVAC#抽象类,语言 javac#编程基础类