首页 > 其他分享 >29.自定义异常

29.自定义异常

时间:2023-03-22 11:34:53浏览次数:43  
标签:自定义 MyException System 29 异常 public out

自定义异常

public class MyException extends Exception{ //继承

    //传递数 >10抛出异常

    private int detail;

    public MyException(int a){

        this.detail = a;

    }

    //alt + insert -> toString:异常的打印信息
    @Override
    public String toString() { //重写方法
        return "MyException{" + detail + '}';
    }
}
public class Test {

    //可能会存在异常的方法
    static void test(int a) throws MyException {

        System.out.println("传递的参数为" + a);

        if(a > 10){
            throw new MyException(a);
        }

        System.out.println("ok");
    }

    public static void main(String[] args) {

        try {
            test(11);
        } catch (MyException e) {
            System.out.println("MyException ->" + e);
        }

    }
}

标签:自定义,MyException,System,29,异常,public,out
From: https://www.cnblogs.com/Zz1001/p/17243079.html

相关文章

  • 28.捕获和抛出异常
    捕获和抛出异常五个关键字:try、catch、finally、throw、throwsinta=1;intb=0;try{//try可以监控区域System.out.println(a/b);}catch(Arithmeti......
  • 27.异常
    异常1.什么是异常?软件程序在运行过程中,有可能会出现各种问题,我们称之为异常,Exception。异常通常发生在程序运行期间,它会影响正常的程序执行流程。2.异常体系结构Java......
  • 一统天下 flutter - 动画: 自定义 Tween - ColorTween, CurveTween, SizeTween, RectT
    一统天下flutterhttps://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-动画:自定义Tween-ColorTween,CurveTween,SizeTween,RectTween,A......
  • Nginx异常信息 upstream timed out (110: Connection timed out) while reading respo
    upstreamtimedout(110:Connectiontimedout)whilereadingresponseheaderfromupstreamNginx代理配置如下:###proxysettingsstartproxy_http_version1.1;p......
  • 自定义类型详解
    一、结构体在C语言中有int,char,float等等类型,可以用来形容某些数据,但是有些数据仅靠一种类型无法描述出来,比如说一个人,我们不仅要描述他的名字,还要描述他的身高、体重、性别......
  • java异常相关问题
    java中有哪些RunTimeException异常RuntimeException           是那些可能在Java虚拟机正常运行期间抛出的异常的超类,是所有运行时异常的顶级接......
  • fastadmin 添加自定义按钮
    更新1.index.html2.JS文件3.require-table.js文件......
  • 题解 ABC294G【Distance Queries on a Tree】
    DFS序树状数组。不妨以\(1\)为根,设\(\operatorname{dep}(u)\)表示\(u\)到根路径的边权和,\(\operatorname{dis}(u,v)\)表示\(u,v\)间路径的边权和,\(\operatornam......
  • Fastadmin 列表自定义按钮
    Fastadmin列表自定义按钮FastAdmin是一款基于ThinkPHP+Bootstrap的极速后台开发框架。文章目录前言一、单纯的调用接口按钮二、打开新的弹窗页面总结前言Fastadmin列表......
  • .net 自定义转换器JsonConverter的使用
    参考官方文档场景描述例如api返回了以下json串(infcode的值有可能时string也可能时number有时候返回时这个{ "infcode":-1, "detail_msg":null}有时后也可能时这个......