Java中自定义异常
2016-07-10 16:29
429 查看
自定义异常
也就是自己写的一个异常类,通常根据自己的业务需求来定义异常,也方便自己的项目中异常的打印和获取。基础异常类
package com.cloud.exception; /** * 构建一个基础的异常类 */ public class DefineException extends RuntimeException{ //VersionUID private static final long serialVersionUID = 3042686055658047285L; //错误位置 protected String positionName; //错误标签 protected String errorLabel; //错误信息 protected String message = ""; public String getMessage(){ return message; } //封装属性 public void setNameAndMessage(String errorLabel,String message){ this.errorLabel = errorLabel; this.message = message; } public void setPositionName(String positionName) { this.positionName = positionName; } public void setErrorLabel(String errorLabel) { this.errorLabel = errorLabel; } public void setMessage(String message) { this.message = message; } public String getErrorLabel(){ return this.errorLabel; } public String getPositionName(){ return this.positionName; } //参照Throwable源码的几个构造方法 public DefineException(){ super(); } public DefineException(Throwable throwable){ super(throwable); } public DefineException(String errorLabel){ super(errorLabel); this.errorLabel = errorLabel; this.message = errorLabel; } public DefineException(String positionName,String errorLabel){ super(errorLabel); this.errorLabel = errorLabel; this.positionName = positionName; this.message = errorLabel; } public DefineException(String positionName,Throwable throwable){ super(throwable); this.positionName = positionName; } public DefineException(String positionName,String errorName,Throwable throwable){ super(throwable); this.errorLabel = errorName; this.positionName = positionName; this.message = errorName; } } |
扩展一个使用的异常类
package com.cloud.exception; /** * 扩展一个自己使用的异常类 */ public class MyException extends DefineException{ private static final long serialVersionUID = -3042686055658047285L; public MyException(){} public MyException(Throwable throwable){ super(throwable); } public MyException(String positionName,String errorLabel){ super(positionName, errorLabel); } public MyException(String errorLabel){ super(errorLabel); } public MyException(String positionName,Throwable throwable){ super(positionName, throwable); } public MyException(String positionName,String errorLabel,Throwable throwable){ super(positionName, errorLabel, throwable); } } |
测试MyException
package com.cloud.exception; /** * 测试自己定义的异常 */ public class TestException { public static void main(String[] args) { try { String name = new TestException().getName("cloud"); System.out.println(name); } catch (MyException e) { String pisotionName = e.getPositionName(); String errorLabel = e.getErrorLabel(); String errorMessage = e.getMessage(); System.out.println("pisotionName:"+pisotionName); System.out.println("errorLabel:"+errorLabel); System.out.println("errorMessage:"+errorMessage); } catch (Exception e) { System.out.println("出现异常..."); e.printStackTrace(); } } public String getName(String name){ MyException me = new MyException(); if(name.equals("cloud")){ me.setPositionName(this.getClass().getName()); me.setMessage("姓名无法通过"); me.setErrorLabel("参数错误"); throw me; }else{ return "Spring"; } } } |
测试结果
pisotionName:com.cloud.exception.TestException errorLabel:参数错误 errorMessage:姓名无法通过 |
相关文章推荐
- springmvc之默认错误页面跳转
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- java-1.5-1.9新特性
- 接口
- maven+springmvc+dubbo的简单配置
- Java对象序列化
- 选择排序-java实现
- MQTT和spring的整合(生产者)
- java 加密算法 base64 以及 自定义 加解密
- 浅谈JavaEE中的Hibernate中的四种关系映射(三)
- Spring Security(20)——整合Cas
- Spring Security(19)——对Acl的支持
- 理解Java基础之注解Annotation
- MQTT和spring的整合(消费端)
- 《java多线程》--利用线程池来模拟选课
- java项目显示红叉,程序却没有错误
- java项目显示红叉,程序却没有错误
- java项目显示红叉,程序却没有错误
- java项目显示红叉,程序却没有错误
- java项目显示红叉,程序却没有错误