全局异常

package com.hr.exeception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**

  • 全局异常处理

*/
@ControllerAdvice
public class GlobleHandlerExeception {

/*
* 全局数据绑定
* */

@ModelAttribute(name = "mo1")
public Map<String,Object> modelAtrrbute2(){
    HashMap<String, Object> map = new HashMap<>();
    map.put("name","lisi");
    map.put("age",21);
    return map;
}
@ModelAttribute
public Map<String,Object> modelAtrrbute(){
    HashMap<String, Object> map = new HashMap<>();
    map.put("name","zhangsan");
    map.put("age",18);
    return map;
}

/*
* 异常处理
* */
@ExceptionHandler(Exception.class)
public void testExeception(Exception e){
    System.out.println("未知异常");
}

@ExceptionHandler(RuntimeException.class)
public void testExeception0(RuntimeException e){
    System.out.println("运行时异常");
}
@ExceptionHandler(ArithmeticException.class)
public void testExeception1(ArithmeticException e){
    System.out.println("除零异常");
}

@ExceptionHandler(ArrayIndexOutOfBoundsException.class)
public void testExeception2(ArrayIndexOutOfBoundsException e){
    System.out.println("数组下标越界");
}

}

上一篇:iLogtail 入门-采集Telegraf Metrics 数据


下一篇:mac系统使用技巧链接汇总