编写自己的Exception
2015-07-07 14:24
134 查看
在实际的工作中,通常需要定义自己功能模块相关的异常,下面一个实例可以实现定义自己的异常:
1.定义一个异常码抽象类作为基类
public abstract class BaseExceptionCode { private String errorMessage ; private int errorCode ; private final BaseExceptionCode display ; public BaseExceptionCode(String errorMessage,int errorCode){ this.errorMessage = errorMessage; this.errorCode = errorCode ; this.display = this ; } public BaseExceptionCode(int errorCode,String errorMessage){ this.errorCode = errorCode ; this.errorMessage = errorMessage ; this.display = this ; } public String getErrorMessage(){ return errorMessage ; } public int getErrorCode(){ return errorCode ; } public BaseExceptionCode getDisplay(){ return display ; } }
2.定义自己的异常码
public class MyExceptionCode extends BaseExceptionCode{ protected MyExceptionCode(String errorMessage,int errorCode){ super(errorMessage,errorCode); } protected MyExceptionCode(int errorCode,String errorMessage){ super(errorCode,errorMessage); } public static final MyExceptionCode E_PARAM_IS_NULL_A = new MyExceptionCode(ConstantWithExceptionMsg.E_PARAM_NULL,ConstantWithExceptionCode.E_PARAM_CODE); public static final MyExceptionCode E_PARAM_IS_NULL_B = new MyExceptionCode(ConstantWithExceptionCode.E_PARAM_CODE,ConstantWithExceptionMsg.E_PARAM_NULL); }
3.定义自己的异常码集合(常量)
/** * 异常码集合 */ public class ConstantWithExceptionCode { public static final int E_PARAM_CODE = 100001 ; }
4.定义自己的异常信息集合(常量)
/** * 异常信息 */ public class ConstantWithExceptionMsg { public static final String E_PARAM_NULL = "参数为空" ; }
5.定义自己的异常
public class MyException extends RuntimeException{ private static final long serialVersionUID = -1933116620478022916L; private String errorMessage ; //异常信息 private MyExceptionCode errorCode ; //异常码 public MyException(MyExceptionCode errorCode){ super(errorCode.getErrorMessage()); this.errorCode = errorCode ; this.errorMessage = errorCode.getErrorMessage(); } public MyException(String errorMessage,MyExceptionCode errorCode){ super(errorMessage); this.errorMessage = errorMessage ; this.errorCode = errorCode ; } public MyException(MyExceptionCode errorCode,String errorMessage){ super(errorMessage); this.errorCode = errorCode; this.errorMessage = errorMessage; } public String getErrorMessage(){ return errorMessage ; } public MyExceptionCode getErrorCode(){ return errorCode ; } }
6.测试自己的异常
public class MyExceptionTest { public static void main(String[] args){ try{ test("",""); }catch(MyException e){ System.out.println(e); throw e ; } } private static void test(String str1,String str2) throws MyException{ if("".equals(str1) || str1 == null || "".equals(str2) || str2 == null ){ throw new MyException(MyExceptionCode.E_PARAM_IS_NULL_A); }else{ return ; } } }
7.测试结果
com.zh.exception.MyException: 参数为空
Exception in thread "main" com.zh.exception.MyException: 参数为空
at com.zh.exception.MyExceptionTest.test(MyExceptionTest.java:16)
at com.zh.exception.MyExceptionTest.main(MyExceptionTest.java:6)
相关文章推荐
- 编写自己的Exception
- 自己用gtk编写的可视化的gdb:gdbv0.54 版本即将发布!
- 从零开始编写自己的C#框架(26)——小结
- Kafka 编写自己的producer、partitioner和consumer
- android 圆角编写(懒得去找,写给自己看的)
- 自己编写 shell 命令 实现安全的删除文件
- 自己编写的http启动脚本2
- centOS系统中为自己编写的c程序添加开机自启动
- 如何自己编写Makefile
- 自己动手编写一个VS插件(五)
- 在 C# 中加载自己编写的动态链接库
- CSS 开始逐渐的喜欢编写CSS样式了 这里记录下自己的一些小小收获
- 如何编写自己的equals方法
- 自己编写的类似JS的trim方法
- 从零开始编写自己的C#框架(8)——后台管理系统功能设计
- 给大家分享一款自己编写的“智能增量网站快速同步”软件
- 编写自己的Shell解释器
- GDAL源码剖析(十)之编写自己的扩展格式
- VC++6.0编写、引用自己的头文件
- 使用ALSA编写自己的音频程序