黑马程序员技术交流社区

标题: 新人求帮助,自定义异常,为啥就出问题了呢? [打印本页]

作者: 阿阿阿阿兵    时间: 2017-3-24 16:52
标题: 新人求帮助,自定义异常,为啥就出问题了呢?
package com.day0323.ExceptionPractise;

public class WorkDemo {
        public static void main(String[] args) {
                Car car = new Car();
                Worker w = new Worker(car);
                try {
                        w.gotoWork();
                        System.out.println("上班没有迟到");
                } catch (LateException e) {
                        e.printStackTrace();
                        System.out.println("上班迟到");
                }
        }
}
class Car{
        private int x = 2;
        public void run(){
                if (x == 2) {
                        throw new CarWrongException("车抛锚了");
                }
                System.out.println("开车上班");
        }
}

class Worker{
        Car car = null;
        Worker(Car car){
                this.car = car;
        }
        public void gotoWork(){
                try {
                        car.run();
                        System.out.println("正常上班");
                } catch (CarWrongException e) {
                        e.printStackTrace();
                        this.walk();
                        throw new LateException("迟到原因:"+e.getMessage());
                }
        }
        private void walk() {
                System.out.println("走路去上班");
        }
}
class CarWrongException extends RuntimeException {

        private static final long serialVersionUID = 1L;

        public CarWrongException() {
                super();
        }

        public CarWrongException(String arg0, Throwable arg1) {
                super(arg0, arg1);
        }

        public CarWrongException(String arg0) {
                super(arg0);
        }
       
}
  class LateException extends Exception {

                private static final long serialVersionUID = 1L;

                public LateException() {
                        super();
                }

                public LateException(String arg0, Throwable arg1) {
                        super(arg0, arg1);
                }

                public LateException(String arg0) {
                        super(arg0);
                }

        }
作者: 阿阿阿阿兵    时间: 2017-3-24 16:53
求大神帮看看,到底是哪里出问题了~
作者: kevin0    时间: 2017-3-24 18:13
太多了,眼都花了,你直接来个int a=1/0就好了





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2