java异常类的妙用
以往在使用异常时,只是知道通过异常类的构造方法设置一些出错信息,此外最多就是把引起该异常的原因通过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异常类的妙用的更多相关文章
- 面试准备(三) Java 异常类层次结构
在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中.这类容易出选择题 考试你是否掌握了异常类并清楚哪些异常类必须捕获 下面的图展示了Java异常类的继承关系. 图1 粉红色的 ...
- Java 异常类层次结构
在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中. 下面的图展示了Java异常类的继承关系. 图1 粉红色的是受检查的异常(checked exceptions),其必须被 ...
- java——异常类、异常捕获、finally、异常抛出、自定义异常
编译错误:由于编写程序不符合程序的语法规定而导致的语法问题. 运行错误:能够顺利的编译通过,但是在程序运行过程中产生的错误. java异常类都是由Throwable类派生而来的,派生出来的两个分支分别 ...
- java异常类
package shb.java.exception; /** * 测试异常类 * @Package:shb.java.exception * @Description: * @author shao ...
- Java异常类(Throwable)
一.异常类体系 二.异常类由来与定义 [异常类的由来]:Java把程序在运行时出现的各种不正常情况也看成了对象, 提取属性和行为进行描述,比如异常名称,异常信息,异常发生位置,从而形成了各种异常类 [ ...
- Java异常类复习总结
个人理解先行: 异常类是当在程序出现问题时抛出的一个警告.提示你程序设计或者代码有存在错误的地方. 异常类和Error都继承自Throwable, Throwable继承自Object类. Runti ...
- 课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类
异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况.许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象.例如:数组越界和被0除. 代码验证: package test ...
- Java异常类及处理
异常概述:运行时发生的不正常情况 在java中用类的形式对不正常的情况进行了描述和封装对象. 描述不正常的类,称之为异常类. 异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述 ...
- java -> 异常类与自定义异常
异常 什么是异常?Java代码在运行时期发生的问题就是异常. 在Java中,把异常信息封装成了一个类.当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置.原因等). 异常的继承体 ...
随机推荐
- Redis数据类型的基本操作
Redis数据类型的基本操作 一.string类型 1.设置value
- 牛客Wannafly挑战赛13-BJxc军训-费马小定理、分式取模、快速幂
参考:https://blog.csdn.net/qq_40513946/article/details/79839320 传送门:https://www.nowcoder.com/acm/conte ...
- poj1273 Drainage Ditches (最大流板子
网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...
- Codeforces 919D Substring (拓扑排序+树形dp)
题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个 ...
- CodeForces 590C Three States BFS
Three Statesy 题解: 以3个大陆为起点,都dfs一遍,求出该大陆到其他点的最小距离是多少, 然后枚举每个点作为3个大陆的路径交点. 代码: #include<bits/stdc++ ...
- 牛客网暑期ACM多校训练营(第二场) I Car 思维
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...
- FZU Tic-Tac-Toe -.- FZU邀请赛 FZU 2283
Problem L Tic-Tac-Toe Accept: 94 Submit: 184Time Limit: 1000 mSec Memory Limit : 262144 KB Pr ...
- 深入分析Mybatis 使用useGeneratedKeys获取自增主键
摘要 我们经常使用useGenerateKeys来返回自增主键,避免多一次查询.也会经常使用on duplicate key update,来进行insertOrUpdate,来避免先query 在i ...
- mysql 主主从配置
配置主服务器:主服务器1 Ip: 192.168.0.1 主服务器2 Ip: 192.168.0.2 主服务器1配置 2.1.修改mysql配置文件 vim /etc/my.conf Server ...
- Spring 两大核心 IOC 和 AOP
如果你的简历上写着Spring (请详述一下spring的两大核心)这个问题一定会被问到. 一.什么叫IOC 1. IOC 全称(Inversion of Control)-- 控制反转. IOC 只 ...