码迷,mamicode.com
首页 > 编程语言 > 详细

Java自定义Exception

时间:2014-12-02 12:14:01      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

这里总结一下Java的Exception,并实现一个自定义Exception类。

总结:

  1. Throwable对象分为两种类型:Error(表示编译时和系统错误);Exception(在Java类库、用户方法以及运行时故障中抛出的异常)。
  2. Exception细分成两种异常:受检查异常(如,IOException, SQLException等。在编译时要被强制检查的异常,需要用try, catch, finally关键字在编译时期处理,否则会报错。);运行时异常(ArithmeticException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException, NullPointerException等。编译器不会检查这类异常。)
  3. Java编程中处理异常的最佳实践[4]:
    • 为可恢复的错误使用检查型异常,为编程错误使用非检查型错误。
    • 在finally程序块中关闭或者释放资源
    • 在堆栈跟踪中包含引起异常的原因
    • 始终提供关于异常的有意义的完整的信
    • 避免过度使用检查型异常
    • 将检查型异常转为运行时异常
    • 记住对性能而言,异常代价高昂
    • 避免catch块为空
    • 使用标准异常
    • 记录任何方法抛出的异常
  4. 受检查异常和运行时异常的区别:
    • 受检查异常需要自行处理,运行时异常不需要
    • 受检查异常是Exception直接子类,运行时异常是RuntimeException的子类
    • 受检查异常多是编程时出现的错误,运行时异常是程序运行时故障率较高
  5. 避免出现NullPointerException的最佳实践[5]:
    • 用已知的字符串对象调用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.");

	}
}



参考资料:

1. JDK1.7 API:http://docs.oracle.com/javase/7/docs/api/

2. Java编程思想(第四版)

3. Effective Java

4. Exception Handling Java Best Practices: 
http://javarevisited.blogspot.com/2013/03/0-exception-handling-best-practices-in-Java-Programming.html

5. Java Tips and Best practices to avoid NullPointerException: 
http://javarevisited.blogspot.com/2013/05/java-tips-and-best-practices-to-avoid-nullpointerexception-program-application.html


说明:

如有错误还请各位指正,欢迎大家一起讨论给出指导。

最后更新时间:2014-12-02


Java自定义Exception

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://my.oschina.net/liuzeli/blog/351280

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!