Yuan-.- 2021-10-02 22:20 采纳率: 100%
浏览 28
已结题

Java如何捕获异常!类型不匹配如何捕获异常鸭!

public class Test extends Exception{
public static void main(String[] args) throws Exception {

        try {
            MyDate d1 = new MyDate(1999, 1, 1);
            MyDate d2 = new MyDate(2009, 4, 1);
            MyDate d3 = new MyDate(a, 1, 1);
            MyDate d4=new MyDate(-1, 13, 32);
            d1.print();
            d2.print();
            d3.print();
            d4.print();
        }catch (Exception e) {
            throw new Exception("\n自定义异常类!");
        }
}

}

请问,如果我要捕获这个d3创建对象错误,数据类型不匹配,如何捕获呢,我试了很多方法,都不能成功!
不会显示我要输出的语句。直接出现系统错误。

img

程序的类代码:

public class MyDate extends  Exception{
    //属性:
    private int year,month,day;
    boolean isRunyear;
    //构造器:
    public MyDate(){
        this.year=1999;
        this.month=1;
        this.day=1;
    }
    public MyDate(int year, int month, int day){
            try {
                setYear(year);
                setMonth(month);
                setDay(day);
            }catch (Exception e){
                System.out.println("Error!");
            }

    }
    //方法:
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
            this.year = year;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
            this.day = day;
    }
    public String isRunyear(){
        if(year%4==0&&year%100!=0){
            isRunyear=true;
        }
        if(isRunyear==true) {
            return "闰年!";
        } else{
            return "不是闰年!";
        }
    }
    public void print(){
        if(year<0) {
            System.out.println("年份不能为负数!");
        }else {
            System.out.println(getYear() + "年" + getMonth() + "月" + getDay() + "日  " + isRunyear());
        }
    }
}

  • 写回答

1条回答 默认 最新

  • weixin_42346558 2021-10-02 23:02
    关注

    你这不是异常问题了,找不到a是编译都不通过,运行都没运行
    对于异常,可以考虑

    public void print(){
            if(year<0) {
                System.out.println("年份不能为负数!");
            }else {
                System.out.println(getYear() + "年" + getMonth() + "月" + getDay() + "日  " + isRunyear());
            }
        }
    
    // 抛出java内部异常,定义信息
    public void print(){
            if(year<0) {
               throws new RuntimeException("年份不能为负数!")
            }else {
                System.out.println(getYear() + "年" + getMonth() + "月" + getDay() + "日  " + isRunyear());
            }
        }
    
    // 自定义异常,对于DateEroorException需要自己实现,继承RuntimeException即可
    public void print(){
            if(year<0) {
               throws new DateEroorException("年份不能为负数!")
            }else {
                System.out.println(getYear() + "年" + getMonth() + "月" + getDay() + "日  " + isRunyear());
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月11日
  • 已采纳回答 10月3日
  • 创建了问题 10月2日

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记