java第7天代码 (java异常)

/*****************TestCar1  begin************************/

public class TestCar1 {
 
 public static final int Ok=1;     //正常情况
 public static final int Wrong=2;  //异常情况
 
 public int run(){
  if("车子没有出现故障"){            //正常流程
   return Ok;               
  }else {                       //异常流程
   return Wrong;
  }
 }
}
class Worker{
 private TestCar1 car;
 
 public static final int int_time=1;     //正常情况,准时到达地点
 public static final int late=2;         //异常情况,上班迟到
 
 public Worker(){}
 
 /** 开车去上班 */
 public int gotoWork(){
  if(car.run()==TestCar1.Ok){
   return int_time;
  }else{
   walk();
   return late;
  }
 }
 /** 步行去上班 */
 public void walk(){}
}

/*****************TestCar1  end************************/

 

/*****************TestCar2  begin************************/

import java.util.Date;

public class TestCar2 {

}

/** 表示车子出现故障情况类  */
class CarWrongException extends Exception{
 public CarWrongException(){}
 
 public CarWrongException(String message){
  super(message);
 }
}
/** 表示上班迟到情况类  */
class LateException extends Exception{
 
 private Date arriveTime;    //迟到的时间
 private String reason;      //迟到的原因
 
 public Date getArriveTime() {
  return arriveTime;
 }

 public String getReason() {
  return reason;
 }

 public void setArriveTime(Date arriveTime) {
  this.arriveTime = arriveTime;
 }

 public void setReason(String reason) {
  this.reason = reason;
 }

 public LateException(Date arriveTime, String reason) {
  this.arriveTime = arriveTime;
  this.reason = reason;
 } 
}
class Car{
 public void run(){
  //如果车子出现故障,就创建一个CarWrongException对象,将其抛出
  if(车子无法刹车)throw new CarWrongException("车子无法刹车");
  if(发动机无法启动)throw new CarWrongException("发动机无法启动");
 }
}
class Worker1{
 private Car car;
 
 public Worker1(Car car){this.car=car;}
 
 public void gotoWork()throws LateException,CarWrongException{
  try{
   car.run();
  }catch(Exception e){  //处理车子出现的异常情况
   walk();
   Date date=new Date(System.currentTimeMillis());
   String reason=e.getMessage();
   throw new LateException(date,reason);//创建一个LateException对象,并将其抛出
  }
 }
 /** 步行去上班 */
 public void walk(){}
}

 

/*****************TestCar2  end************************/

 

/*****************以上代码没有具体的实现方法,不能直接使用************************/

 

/*****************Test.java begin************************/

 

import java.io.FileInputStream;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        FileInputStream in = null;
        try {
            in = new FileInputStream("myfile.txt");
            int b;
            b = in.read();
            while (b != -1) {
                System.out.print((char) b);
                b = in.read();
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            System.out.println("finally.....");
            //关闭文件操作
        }
    }
}

/*****************Test.java end************************/

 

/*****************Test2.java begin************************/

 

public class Test2 {
    public static void main(String args[]){
        String[][] s = new String[5][];
  try {
            s[5] = new String[3];
            s[5][1] = "hello";
        } catch(NullPointerException e2){
            System.out.println("有异常发生了");
            e2.getMessage();
            e2.printStackTrace();
        } catch(Exception e1) {
         System.out.println("数组下标越界了");
         e1.getMessage();
         e1.printStackTrace();
        }finally{
         System.out.println("不管怎么样我都会执行。。。");
        }
    }
}

 

/*****************Test2.java end************************/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值