异常类的妙用

 

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

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

下面是在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;
       }
   }

通过上述两段源码可以看到,老外在写代码时考虑得很细,在异常类中加入了额外信息后,还提供了一个 clearExtraInformation()方法,用来清除额外信息。

转:https://www.cnblogs.com/hzhuxin/archive/2011/12/12/2285101.html

java异常类的妙用的更多相关文章

  1. 面试准备(三) Java 异常类层次结构

    在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中.这类容易出选择题 考试你是否掌握了异常类并清楚哪些异常类必须捕获 下面的图展示了Java异常类的继承关系. 图1 粉红色的 ...

  2. Java 异常类层次结构

    在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中. 下面的图展示了Java异常类的继承关系. 图1 粉红色的是受检查的异常(checked exceptions),其必须被 ...

  3. java——异常类、异常捕获、finally、异常抛出、自定义异常

    编译错误:由于编写程序不符合程序的语法规定而导致的语法问题. 运行错误:能够顺利的编译通过,但是在程序运行过程中产生的错误. java异常类都是由Throwable类派生而来的,派生出来的两个分支分别 ...

  4. java异常类

    package shb.java.exception; /** * 测试异常类 * @Package:shb.java.exception * @Description: * @author shao ...

  5. Java异常类(Throwable)

    一.异常类体系 二.异常类由来与定义 [异常类的由来]:Java把程序在运行时出现的各种不正常情况也看成了对象, 提取属性和行为进行描述,比如异常名称,异常信息,异常发生位置,从而形成了各种异常类 [ ...

  6. Java异常类复习总结

    个人理解先行: 异常类是当在程序出现问题时抛出的一个警告.提示你程序设计或者代码有存在错误的地方. 异常类和Error都继承自Throwable, Throwable继承自Object类. Runti ...

  7. 课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类

    异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况.许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象.例如:数组越界和被0除. 代码验证: package test ...

  8. Java异常类及处理

    异常概述:运行时发生的不正常情况 在java中用类的形式对不正常的情况进行了描述和封装对象. 描述不正常的类,称之为异常类. 异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述 ...

  9. java -> 异常类与自定义异常

    异常 什么是异常?Java代码在运行时期发生的问题就是异常. 在Java中,把异常信息封装成了一个类.当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置.原因等). 异常的继承体 ...

随机推荐

  1. Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)

    Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...

  2. ES6之模块化导入导出

    1.概述 在js的历史上一直没有模块(module)体系,无法将一个大程序拆分成相互依赖的小文件,再用简单的方法拼装起来,这对开发大型的.复杂的项目形成了巨大障碍. 在 ES6 之前,社区制定了一些模 ...

  3. html基础——下拉式菜单

    一个网站能否让用户容易使用该网站往往是由菜单栏体现出来,因为它为网页的大多数页面提供功能入口.一个轻轻的点击以后,即可显示出菜单项,将网站的大部分页面和功能显示出来让用户清楚了解从而用户节约一定的时间 ...

  4. 江苏 徐州邀请赛 icpc B Array dp 滚动数组模板

    题目 题目描述 JSZKC is the captain of the lala team. There are N girls in the lala team. And their height ...

  5. 05 python内置函数大全

    一.数学运算 abs:返回数字的绝对值 abs(-1) round:保留浮点数的小数位数,默认保留整数.四舍五入. round(1.553,1) #1.6 divmod:计算除数和被除数的结果,并返回 ...

  6. Go语言标准库之strconv

    Go语言中strconv包实现了基本数据类型和其字符串表示的相互转换. strconv包 strconv包实现了基本数据类型与其字符串表示的转换,主要有以下常用函数: Atoi().Itia().pa ...

  7. ConcurrentHashMap 的工作原理及代码实现

    ConcurrentHashMap采用了非常精妙的"分段锁"策略,ConcurrentHashMap的主干是个Segment数组.Segment继承了ReentrantLock,所 ...

  8. spring boot启动加载项CommandLineRunner

    spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...

  9. JSP学习笔记(5)——Servlet、监听器、过滤器、MVC模式介绍

    MVC模式 在讲解Servlet前,先介绍一下MVC模式. M:model 模型,相当于数据层,用于存放数据,如一个Java中的一个bean类 V:view 视图,相当于页面层,用于显示数据,如一个网 ...

  10. sql 增删改列名

    添加列:alter table table_name add new_column data_type [interality_codition] ALTER TABLE dbo.tb newColu ...