算数异常类:ArithmeticExecption
空指针异常类型:NullPointerException
类型强制转换类型:ClassCastException
数组负下标异常:NegativeArrayException
数组下标越界异常:ArrayIndexOutOfBoundsException
违背安全原则异常:SecturityException
文件已结束异常:EOFException
文件未找到异常:FileNotFoundException
字符串转换为数字异常:NumberFormatException
操作数据库异常:SQLException
输入输出异常:IOException
方法未找到异常:NoSuchMethodException
下标越界异常:IndexOutOfBoundsExecption
系统异常:SystemException
创建一个大小为负数的数组错误异常:NegativeArraySizeException
数据格式异常:NumberFormatException
安全异常:SecurityException
以下代码将异常直接返回以便查看,实际可写入日志,未完待续
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import lombok.extern.slf4j.Slf4j;
@ControllerAdvice
@Slf4j
public class RestExController {
@ResponseBody
@ExceptionHandler(Exception.class)
public Map exceptionHandler(Exception e) {
Map map = new HashMap<>();
int j = 0;
map.put("exception info",e.toString());
for(int a = 0; a< e.getStackTrace().length;a++) {
StackTraceElement st = e.getStackTrace()[a];
Map map1 = new HashMap<>();
String aa =e.getStackTrace()[a].getClassName();
if(aa.contains("com.example.demo")) {
j++;
map1.put("location "+j+": ", st.getClassName() + ", linenumber: "+st.getLineNumber());
}
map.putAll(map1);
}
map.put("exception", e.getClass());
return map;
}
}
自定义
package com.example.demo.controller;
public class MyException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyException() {
super();
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(String message) {
super(message);
}
public MyException(Throwable cause) {
super(cause);
}
}