• 自定义异常类


    1自定义异常类方法:

    1) 自定义异常类继承现有的异常类

    2 )提供一个序列号,提供几个重载的构造器

    2 子类重写父类的方法,其抛出的异常类型只能使被重写的方法的异常类的子类或和异常类型一样

    3)throw是在手动抛出异常时使用,throws是当声明异常时使用

    package lianxi3;
    
    public class EcmDef {
    
        public static void main(String[] args) {
          try{
            int i = Integer.parseInt(args[0]);
            int j = Integer.parseInt(args[1]);
            ecm(i,j);
          }catch(NumberFormatException e){
              System.out.println("输入的数据类型不一致");
          }
          catch(ArrayIndexOutOfBoundsException e){
              System.out.println("缺少命令行参数");
          }
          catch(ArithmeticException e){
              System.out.println("分母为0");
          }
          catch(EcMinus ec){
              System.out.println(ec.getMessage());
          }
        }
        public static void ecm(int i,int j) throws EcMinus{    
             if(i<0||j<0){
                throw new EcMinus("输入的数不能有负数");  //不是运行异常,必须显式表示
            }
            System.out.println(i/j);
        }
    }
    class EcMinus extends Exception{
        static final long serialVersionUID = -33875229948L;
        public EcMinus() {
            
            }
        public EcMinus(String msg){
            super(msg);
        }
    }
  • 相关阅读:
    Springboot(一)springboot简介与入门程序
    Java基础(二)流程语句与数组
    Java基础(一)基础常识
    Kubernetes介绍
    K8s 存储
    Kubernetes 集群组件
    Kubernetes 设计理念
    Kubernetes 部署与实战
    容器技术与资源隔离
    Docker 网络配置
  • 原文地址:https://www.cnblogs.com/yjtm53/p/4145212.html
Copyright © 2020-2023  润新知