Java自定义Exception

简介: 国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。

国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html
内部邀请码:C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为中国PE第一股,市值超1000亿元。 

------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

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

这里总结一下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块为空
  • 使用标准异常
  • 记录任何方法抛出的异常
受检查异常和运行时异常的区别:
  • 受检查异常需要自行处理,运行时异常不需要
  • 受检查异常是Exception直接子类,运行时异常是RuntimeException的子类
  • 受检查异常多是编程时出现的错误,运行时异常是程序运行时故障率较高
避免出现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 内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

目录
相关文章
|
1月前
|
Java Spring 容器
【Java】Spring如何扫描自定义的注解?
【Java】Spring如何扫描自定义的注解?
33 0
|
19天前
Exception in thread “main“ java.lang.NoClassDefFoundError: freemarker/template/Configuration
Exception in thread “main“ java.lang.NoClassDefFoundError: freemarker/template/Configuration
18 0
|
3月前
|
XML Java Maven
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
51 0
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
|
5天前
|
Java
Java配置大揭秘:读取自定义配置文件的绝佳指南
Java配置大揭秘:读取自定义配置文件的绝佳指南
11 0
Java配置大揭秘:读取自定义配置文件的绝佳指南
|
19天前
|
Java Maven Spring
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
SpringBoot运行出现 Lookup method resolution failed; nested exception is java.lang.IllegalStateException
25 0
|
22天前
Exception in thread “main“ java.lang.UnsupportedOperationException
Exception in thread “main“ java.lang.UnsupportedOperationException
17 1
|
28天前
|
Java
java 自定义注解 实现限流
java 自定义注解 实现限流
10 1
|
1月前
|
Java 网络安全 开发者
【Docker】5、Dockerfile 自定义镜像(镜像结构、Dockerfile 语法、把 Java 项目弄成镜像)
【Docker】5、Dockerfile 自定义镜像(镜像结构、Dockerfile 语法、把 Java 项目弄成镜像)
41 0
|
2月前
|
Java
[Java]自定义注解
[Java]自定义注解
43 0
|
3月前
|
Java Maven
java使用apache-commons-lang3生成随机字符串(可自定义规则、RandomUtils
java使用apache-commons-lang3生成随机字符串(可自定义规则、RandomUtils
67 0