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

    1. 编写类,该类继承Exception

    public class MyException extends Exception{
        private static final long serialVersionUID = -9146349104405752896L;
        
        public MyException(){
            super();
        }
        
        public MyException(String message){
            super(message);
        }
        
    }

    2. 编写测试类:

    public class TestException {
        
        public static void getAge(int i) throws MyException{
            if(i > 10){
                throw new MyException("不能大于10");
            }
        }
        
        public static void main(String[] args) {
            try {
                getAge(11);
            } catch (MyException e) {
                e.printStackTrace();
            }
        }
    }

    3. 返回结果为:

  • 相关阅读:
    第7章例7-12
    第7章例7-11
    第7章例7-9
    第7章例7-8
    第7章例7-7
    第7章例7-6
    第7章例7-5
    第7章例7-4
    第7章例7-3
    第7章例7-2
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7230044.html
Copyright © 2011-2022 走看看