zoukankan      html  css  js  c++  java
  • Java自定义异常

    class MyException extends Exception//自定义异常,继承自:Exception
    {
        private int num;
        public MyException(String msg,int num)
        {
            super(msg);
            this.num=num;
        }
        public int getNum()
        {
            return this.num;
        }
    }

    class Test
    {
        public static void reg(int num) throws MyException//重点是词句。throws MyException 不可缺少。
        {
            if(num<0)
            {
                throw new MyException("num不可小于0",num);//非法条件,抛出自定义的异常。
            }
            print("reg num::"+num);
        }
        public static void manage()
        {
            try
            {
                reg(100);
            }
            catch (MyException me)//捕获自定义的异常。
            {
                print("feifa num:"+me.getNum());
            }
        }
        public static void main(String[] args)
        {
            Test t=new Test();
            t.manage();
        }

    }

  • 相关阅读:
    centos 配置静态ip
    mysql常用命令
    mac 安装好mysql后密码重置
    安装Intellij Idea14/15
    freemarker 学习一 入门小例子
    获取类路径
    mysql中的int smallint 取值范围
    MySQL按照汉字的拼音排序
    Log4j 基本配置
    追加写入
  • 原文地址:https://www.cnblogs.com/liuwentian/p/3105490.html
Copyright © 2011-2022 走看看