全局异常

简介: 全局异常

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("数组下标越界");
}

}

相关文章
|
12天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1247 5
|
11天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1258 87
|
11天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1814 13
|
21天前
|
人工智能 运维 安全
|
4天前
|
资源调度
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
237 127
|
4天前
|
前端开发
Promise的then方法返回的新Promise对象有什么特点?
Promise的then方法返回的新Promise对象有什么特点?
184 2