这里总结一下Java的Exception,并实现一个自定义Exception类。
总结:
1.Throwable对象分为两种类型:Error(表示编译时和系统错误);Exception(在Java类库、用户方法以及运行时故障中抛出的异常)。
2.Exception细分成两种异常:受检查异常(如,IOException, SQLException等。在编译时要被强制检查的异常,需要用try, catch, finally关键字在编译时期处理,否则会报错。);运行时异常(如,ArithmeticException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException, NullPointerException等。编译器不会检查这类异常。)
3.Java编程中处理异常的最佳实践:
为可恢复的错误使用检查型异常,为编程错误使用非检查型错误。
在finally程序块中关闭或者释放资源
在堆栈跟踪中包含引起异常的原因
始终提供关于异常的有意义的完整的信
避免过度使用检查型异常
将检查型异常转为运行时异常
记住对性能而言,异常代价高昂
避免catch块为空
使用标准异常
记录任何方法抛出的异常
4.受检查异常和运行时异常的区别:
受检查异常需要自行处理,运行时异常不需要
受检查异常是Exception直接子类,运行时异常是RuntimeException的子类
受检查异常多是编程时出现的错误,运行时异常是程序运行时故障率较高
5.避免出现NullPointerException的最佳实践:
用已知的字符串对象调用equals()和equalsIgnoreCase()方法【"hello".equals(objString)】;
如果valueOf()和toString()方法的返回值一样的话,用valueOf()方法代替toString()方法【当为null的对象调用toString()方法时会抛出NullPointerException异常而调用valueOf()方法时会返回一个"null"的封装类】;
使用对null安全的方法和库【eg, StringUtils.isBlank(), isNumeric(), isWhiteSpace()等】;
方法调用的返回值用返回空的集合或者空的数组避免返回null的情况【Collections.EMPTY_LIST, Collections.EMPTY_SET和Collections.EMPTY_MAP等】;
使用@NotNull和@Nullable的注解明确指出是否可能有null的情况出现;
避免代码中出现不必要的自动装箱和拆箱【避免类似的int a 出现接收返回值为null的情况,用Integer a代替更好】;
遵守契约并且使用合理的默认值【通过定义什么可以为空,什么不能为空,主叫方可以明确的作出判断。】;
在使用数据库存储对象时,要定义是否允许某个字段为空,这样数据库自身机制可以检查是否为空的情况,避免程序调用后出现非法的空字段。
使用null的封装对象Null类。
自定义一个Exception类:
ProjectNameException.java 内容如下:
package com.trianlge23.projectname.exception; public class ProjectNameException extends Throwable { private static final long serialVersionUID = 8093803025939797139L; //exception code private int exceptionCode; //exception detailed message private String detailMsg; public ProjectNameException(int exceptionCode, String extraMsg) { super(); this.setDetailMsg(exceptionCode); this.setExtraMsg(extraMsg); } public ProjectNameException(int exceptionCode) { super(); this.setDetailMsg(exceptionCode); } //notice: we do not offer the set method to set the excption code. public int getExceptionCode() { return exceptionCode; } //if there has no extra message for this excption code, init it. private void setDetailMsg(int exceptionCode) { this.exceptionCode = exceptionCode; if (ProjectNameExceptionCode.EXCEPTION_CODE_MAP .containsKey(exceptionCode)) { this.detailMsg = ProjectNameExceptionCode.EXCEPTION_CODE_MAP .get(exceptionCode); } else { this.detailMsg = ProjectNameExceptionCode.EXCEPTION_CODE_MAP .get(ProjectNameExceptionCode.PROJECTNAME_EXCEPTION_CODE_NOT_FOUND); } } //if there has extra message for this exception code, add it. private void setExtraMsg(String extraMsg) { this.detailMsg += ProjectNameExceptionCode.EXTRA_EXCEPTION_MSG_SPLITER + extraMsg; } //override the super class method getMessage() @Override public String getMessage() { return this.detailMsg; } }
ProjectNameExceptionCode.java内容如下:
package com.triangle23.projectname.exception; import java.util.HashMap; import java.util.Map; public class ProjectNameExceptionCode { //the separator between default message and extra message of exception. public static final String EXTRA_EXCEPTION_MSG_SPLITER = ": "; //the map stores the exception code and exception message public static Map<Integer, String> EXCEPTION_CODE_MAP; public static final int PROJECTNAME_EXCEPTION_CODE_NOT_FOUND = 1; static { EXCEPTION_CODE_MAP = new HashMap<Integer, String>(); EXCEPTION_CODE_MAP.put(PROJECTNAME_EXCEPTION_CODE_NOT_FOUND, "[PROJECTNAME Exception] Not found exception code."); } }
发表评论 取消回复