前言:异常处理在日常开发中很常见,一般来讲项目框架中都会对异常处理做一个统一的处理。同时可能会自定义多个错误对象,在业务处理的时候时候抛出来。比如目前采用SSM框架的项目中,就存在多种自定义错误类型,如BizException(业务处理异常)、MessageException(应用系统异常),ValidateException(应用系统校验错误)错误对象.在不同的情况下使用,前面2中业务处理异常及应用系统异常是最常用的,最后一种讲道理应该用到也挺多的,但是平时直接使用MessageException代替使用了,这个坏习惯要改过来.
处理异常错误
日常开发中经常会出现空指针,数组越界,数据类型强转失败,sql语句执行异常等错误,正是由于这些问题的出现所以java需要一种处理异常的解决方式.
- try{}catch(Exception e){} finally{}通过捕获异常并解决
package demo.exception;
import sun.management.resources.agent;
public class Thundering {
public static void main(String[] args) {
//数字格式化异常
String string="20";
try{
int age = Integer.valueOf(string);
System.out.println(age);
}catch (Exception ex) {
// TODO: handle exception
System.out.println("捕获到了异常,处理异常");
int age = Integer.valueOf(string.replaceAll("L", ""));
System.out.println(age);
}finally {
System.out.println("处理完毕");
}
//
}
}
2. throws 在方法层面上 向上抛出,交给调用这个方法的对象去处理,最终还是要捕获并处理这种异常
package demo.exception;
import sun.management.resources.agent;
public class Thundering {
public static void main(String[] args) {
//try catch对于可能存在的异常进行预先捕获并处理,没有异常则不影响代码正常运行
try {
testException();
} catch (ArithmeticException ex) {
System.out.println("调用的方法中 发现异常了 兄弟");
}
}
private static void testException() throws ArithmeticException{
int result=3/0;
System.out.println(result);
}
}
finally代码块的作用
finally的作用在于try{}catch(Exception e){}处理完毕后,都要跟着finally,这是一个完整的异常处理语句的组成.同时需要注意的是在以下4中情况finally不会被执行。
- finally代码块中出现异常
- System.ext()退出程序
- 线程死亡
- 关闭CPU
Java中常见的异常
- ClassCastException 类型转换异常
- ClassNotFoundException 未找到相应的类型
- ArithmeticException 算术运算异常/分母为0的情况
- ArrayIndexOutofBoundsException 数组下标越界
- ArrayStoreException 数组包含不兼容的值异常
- SQLException 操作数据库异常类
- NullPointException 空指针异常
- NoSuchFieldException 字段未找到抛出异常
- NumberFormatException 字符串转换为数字异常
- NegativeArraySizeException 数组元素个数为负数
- StringIndexOutOfBoundsException 字符串索引超过方位
- IOEException 输入输出异常
- IllegalAccessException 不允许访问某类异常
- InstatitationException 当应用程序视图使用class中的newInstance()方法创建一个实例,而指定的类对象无法实例化时,抛出异常]
- EOFEexception 文件已结束异常
- FileNotFoundException
自定义异常
java中可以自定义异常,用于只需要继承Exception异常类即可。实际上当之前提到的MessageException及BizException都是继承Exception的子类RuntimeException异常类.需要注意的是Exception e有三个方法属性
- getMessage()获取错误的性质(平时自定义错误类重写的东西)
- toString()输出错误类型和性质
- printStace()输出错误的完整信息
package demo.exception;
public class MyException extends Exception{
/**
*
*/
private static final long serialVersionUID = 8053693334850681423L;
public MyException(String message) {
//调用父类中的无参构造方法
super(message);
}
}
package demo.exception;
public class Test {
public static void main(String[] args) {
try {
throw new MyException("有错误");
} catch (MyException ex) {
// TODO: handle exception
System.out.println(ex.getMessage());
}
}
}
方法中抛出异常
- throws 方法声明的时候抛出异常,在调用该方法的地方进行处理
- throw 抛出异常, 异常抛出代码停止,平时代码中出现异常了throw new MeesageException()就是这个道理,出现异常spring事物回滚.平时业务代码里面throw抛出异常之后 通过错误解析器解析到了,直接处理返回了。如果像在上一层代码中处理该异常 需要在方法声明中使用throws声明,而我们业务代码中没有是因为业务层调用的地方 已经通过@ControllerAdvice+@ExceptionHanlder的方式控制住了
package com.bxd.core.exception;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.bxd.core.constant.SysErrorConstant;
import com.bxd.core.support.AjaxResult;
@ControllerAdvice(basePackages={"com.bxd.app.controller"})
public class ExceptionResolver{
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionResolver.class);
/**
* 业务异常
* @param e
* @return
*/
@ExceptionHandler({BizException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public Object bizException(BizException e) {
// log记录异常
LOGGER.error("业务处理出错:",e);
AjaxResult result = new AjaxResult();
result.setMsg(e.getMessage());
result.setErrorCode(e.getErrorCode());
return result;
}
/**
* 运行时未知错误
* @param e
* @return
*/
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public Object runException(RuntimeException e){
LOGGER.error("系统内部出错:",e);
AjaxResult result = new AjaxResult();
result.setMsg("内部请求错误");
result.setErrorCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
return result;
}
/**
* 请求超时
* @param e
* @return
*/
@ExceptionHandler(TimeoutException.class)
@ResponseStatus(HttpStatus.REQUEST_TIMEOUT)
@ResponseBody
public Object timeoutException(RuntimeException e){
LOGGER.error("请求超时:",e.getMessage());
AjaxResult result = new AjaxResult();
result.setMsg("请求超时");
result.setErrorCode(String.valueOf(HttpStatus.REQUEST_TIMEOUT.value()));
return result;
}
/**
* 请求权限异常
* @param e
* @return
*/
@ExceptionHandler(UndeclaredThrowableException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ResponseBody
public Object notFoundException(UndeclaredThrowableException e){
LOGGER.error("请求不存在:",e.getMessage());
AjaxResult result = new AjaxResult();
result.setMsg("URL请求不存在");
result.setErrorCode(String.valueOf(HttpStatus.UNAUTHORIZED.value()));
return result;
}
/**
* 应用系统异常
* @param e
* @return
*/
@ExceptionHandler({MessageException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public Object messageException(MessageException e) {
// log记录异常
LOGGER.error("应用系统出错:",e);
AjaxResult result = new AjaxResult();
result.setMsg(e.getMessage());
result.setErrorCode(e.getErrorCode());
return result;
}
/**
* 系统接口校验出错
* @param e
* @return
*/
@ExceptionHandler({ValidateException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public Object validateException(ValidateException e) {
// log记录异常
LOGGER.error("应用系统校验出错:",e.getMessage());
AjaxResult result = new AjaxResult();
result.setMsg(e.getMessage());
result.setErrorCode(e.getErrorCode());
return result;
}
/**
* 系统接口校验出错
* @param e
* @return
*/
@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public Object beanException(Exception e) {
// log记录异常
LOGGER.error("字段校验出错:",e);
AjaxResult result = new AjaxResult();
result.setMsg(SysErrorConstant.SysError.EmptyBean.getErrorMsg()+","+e.getMessage());
result.setErrorCode(SysErrorConstant.SysError.EmptyBean.getErrorCode());
return result;
}
}
运行中时异常
RunTimeException指的就是运行时的异常.继承自Exception类。而Exception实际上是继承自Throwable类的。Exception类包含RunTimeException运行异常和其他异常.一般我们自定义重写的异常就是继承自RunTimeException,另外在自定义异常里面价格String errorcode错误代码(BizException类就是这样的).
BizException自定义错误类
package com.bxd.core.exception;
import com.bxd.app.constant.AppErrorConstant;
public class BizException extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = 1L;
private String errorCode;
public BizException() {
super();
}
public BizException(String errorCode,String message) {
super(message);
this.setErrorCode(errorCode);
}
public BizException(String message) {
super(message);
}
public BizException(AppErrorConstant.AppError error) {
super(error.getErrorMsg());
setErrorCode(error.getErrorCode());;
}
public BizException(String message,Exception e) {
super(message);
}
public BizException(String errorCode,String message, Throwable cause) {
super(message, cause);
this.setErrorCode(errorCode);
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
}
总结
本章主要讲了如何捕获异常 处理异常,以及如何实现自定义异常。对于方法中如何处理异常throws,throw的作用做了介绍.同时像常见的一些错误类型做了一个系统的介绍。最后就是错误类Exception的来源,异常Throwable是Error错误和Exception异常的父类。所有的自定义都是直接或者间接继承这个类的