以往在使用异常时,只是知道通过异常类的构造方法设置一些出错信息,此外最多就是把引起该异常的原因通过Throwable类的子类一同设置进去。今天在分析springSecurity3.0.5的框架时,看到AuthenticationException这个异常类(用在验证时)的源码,发现这个异常类除了上述常见的用法外,还把Authentication(验证信息)的做为属性放到了异常类中,当验证出错时AuthenticationException抛出时,Authentication(验证信息)一同被抛出了,这样捕获异常的类就能得到更丰富的信息,有些数据还可以通过异常类返回给调用者。看了这样的代码,思路不由地开阔了不少。该类源码如下:

 1 package org.springframework.security.core;
 3 /**
 4  * Abstract superclass for all exceptions related to an {@link Authentication} object being invalid for whatever
 5  * reason.
 7  * @author Ben Alex
 9 public abstract class AuthenticationException extends RuntimeException {
10     //~ Instance fields ================================================================================================
12     private Authentication authentication;
13     private Object extraInformation;
15     //~ Constructors ===================================================================================================
17     /**
18      * Constructs an <code>AuthenticationException</code> with the specified message and root cause.
20      * @param msg the detail message
21      * @param t the root cause
22 */
23     public AuthenticationException(String msg, Throwable t) {
24         super(msg, t);
25     }
27     /**
28      * Constructs an <code>AuthenticationException</code> with the specified message and no root cause.
30      * @param msg the detail message
31 */
32     public AuthenticationException(String msg) {
33         super(msg);
34     }
36     public AuthenticationException(String msg, Object extraInformation) {
37         super(msg);
38         this.extraInformation = extraInformation;
39     }
41     //~ Methods ========================================================================================================
43     /**
44      * The authentication request which this exception corresponds to (may be <code>null</code>)
45 */
46     public Authentication getAuthentication() {
47         return authentication;
48     }
50     public void setAuthentication(Authentication authentication) {
51         this.authentication = authentication;
52     }
54     /**
55      * Any additional information about the exception. Generally a <code>UserDetails</code> object.
57      * @return extra information or <code>null</code>
58 */
59     public Object getExtraInformation() {
60         return extraInformation;
61     }
63     public void clearExtraInformation() {
64         this.extraInformation = null;
65     }

下面是在AbstractAuthenticationManager类中被使用到的片断:

public final Authentication authenticate(Authentication authRequest) throws AuthenticationException {
       try {
           return doAuthentication(authRequest);
       } catch (AuthenticationException e) {
           e.setAuthentication(authRequest);
           if (clearExtraInformation) {
               e.clearExtraInformation();
           throw e;