Java系统中异常封装处理
来源:互联网 发布:linux history 参数 编辑:程序博客网 时间:2024/05/23 23:44
异常处理
自定义异常基础类
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); }}
异常处理原则
package com.cloud.Test;import com.cloud.exception.MyException;/** * 异常处理原则: * 1.如果不能处理异常,不要捕获该异常。 * 2.如果要捕获,应在离异常源近的地方捕获它。 * 3.不要捕获的异常,但是什么也不处理。 */public class Test4 {public static void main(String[] args) {MyExe mx = new MyExe();try {/*这里发生异常,则直接离开try代码进入catch*///int i = 1/0;/*这个方法执行扔出的异常就不会被捕获*/mx.getI();}catch(MyException mp){String info = mp.getMessage();System.out.println("捕获自定义异常"+info);} catch (Exception e) {e.printStackTrace();System.out.println("捕获Exception异常");}}}class MyExe{public void getI(){MyException me = new MyException();try {int te = 2/0;} catch (MyException e) {me.setMessage("除数不能为0!");throw me;}catch (Exception e) {System.out.println("Exception异常");e.printStackTrace();}}}
0 0
- Java系统中异常封装处理
- Java系统中时间封装处理
- Java异常处理的基本封装
- Java中异常处理
- Java中异常处理
- java中异常处理
- java中异常处理
- Java中异常处理
- Java中异常处理
- java中处理异常
- java中异常处理
- Java工程项目开发中异常处理的方法及系统
- java应用中,调用第三方api如何处理异常(系统异常、操作异常等等)
- 异常处理模块封装
- java中异常及异常处理
- java中异常处理机制
- JAVA中异常处理问题
- java中异常处理机制
- Trie/Xor
- hdu 5810 优先队列
- 区间dp_2
- Volley与Activity生命周期联动及简单的二次封装
- POJ 1273 Drainage Ditches
- Java系统中异常封装处理
- c# 读取写入excel单元格(包括对excel的一些基本操作)【总结篇】
- git编译安装与常用命令
- tomcat如何配置项目文件夹外的虚拟子目录?
- overload与override的区别
- win系统下nodejs安装及环境配置
- ArrayList和LinkedList add的源码分析
- 玄魂工作室--咪噜妹
- 发布网站