Java异常
分类和关系
从异常的继承关系可以把异常分为ERROR和Exception两类,ERROR为严重异常,一般来说是程序本身无法处理的,开发中常见的内存溢出相关异常大都是ERROR,常见的有
-
OutOfMemoryError: Java heap space —-JVM Heap(堆)溢出
-
OutOfMemoryError: PermGen space —- PermGen space溢出
-
StackOverflowError —- 栈溢出
Exception被称作运行时异常,可以捕获并作出处理(java规定Exception必须被捕获,但不包括RuntimeException)
- NullPointerException 空指针,一般出现在调用了未初始化或不存在的对象
- ClassNotFoundException 类未找到,一般在编译时出现,常见原因一般未缺少包,或则包冲突与程序冲突造成
- SocketException 网络读取失败
- IndexOutOfBoundsException 数组下标越界,一般出现在循环处理异常
常见的RunTimeException,可以从这篇文https://blog.csdn.net/QQ635785620/article/details/7781026获取
一般来说RuntimeException异常不需要trycatch来捕获,从异常的子类可以看出,该类型的异常一般是编译器所无法识别的异常一般称之为非检查性异常
异常的处理
异常的捕获一种是直接trycatch还有一种是向上抛出异常让上一层去处理,这里需要注意的是,异常的内容不应该对内容可见,尤其是未处理的异常一般都为英文,直接不处理展示非常影响体验这里推荐使用 全局异常处理,可以额外整合i18是展示更顺滑。
异常的捕获代码
try {
//运行代码
} catch (Exception e) {
//异常处理
logger.error("异常:"+e.getMessage());//打印异常
throw new Exception(e.getMessage());//如果不行想打印捕获的异常也可以抛出一个自定义的异常,下文会说
}finally{
//必须完成的操作 一般用来断开连接或者关闭流
}
运行try代码块,如果有异常运行catch代码块,再运行finally代码块,其中finally代码块是必定运行的。
异常的处理
可以在需要抛出的方法上标注 throws 指定异常
全局异常处理
springboot 中可以通过以下方式实现
@RestControllerAdvice
public class ExceptionController4JSON {
@ExceptionHandler({ BaseException.class })
public BaseResponseDto baseException(BaseException e) {
LOG.error(e.getMessage(),e);
if (StringUtils.isNoneEmpty(e.getMessage())) {
return new BaseResponseDto(e.getErrCode(),e.getMessage());
}else if(StringUtils.isNoneEmpty(e.getErrCode())){
return new BaseResponseDto(e.getErrCode(),i18n(e.getErrCode()));
}else{
return new BaseResponseDto("MSG_ERR_999999",i18n("MSG_ERR_999999"));
}
}
}
指定区域异常处理
适合那些不想全局处理的
@RestController
public class ExceptionController4JSON {
@ExceptionHandler({ BaseException.class })
public BaseResponseDto baseException(BaseException e) {
LOG.error(e.getMessage(),e);
if (StringUtils.isNoneEmpty(e.getMessage())) {
return new BaseResponseDto(e.getErrCode(),e.getMessage());
}else if(StringUtils.isNoneEmpty(e.getErrCode())){
return new BaseResponseDto(e.getErrCode(),i18n(e.getErrCode()));
}else{
return new BaseResponseDto("MSG_ERR_999999",i18n("MSG_ERR_999999"));
}
}
}
@RestController
@RequestMapping("/push")
public class DataController extends PushExceptionController4JSON
例:
public static <T> List<T> importExcel(File file, Class<?> pojoClass, ImportParams params) throws Exception {
自定义异常
自定义异常可以通过继承Excetion或者RuntionException来实现
例:
/**
* Token异常
*/
public class TokenException extends RuntimeException{
private String code;
private String msg;
private static final long serialVersionUID = -3980220541562671030L;
public TokenException() {
super();
}
public TokenException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public TokenException(String message, Throwable cause) {
super(message, cause);
}
public TokenException(String message) {
super(message);
}
public TokenException(Throwable cause) {
super(cause);
}
public TokenException(String Code, String Msg){
this.code=Code;
this.msg=Msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}