如果我传播一个异常,我是否必须稍后再捕捉它?传播异常必须在Java中被捕获?
假设我有这样的代码:
public class InvalidDataException extends Exception {
public InvalidDataException (String e){
super(e);
}
}
public class Vehicle {
private double speed;
private int vin;
public Vehicle (double nspeed, int nvin) throws InvalidDataException{
setVin(nvin);
setSpeed(nspeed);
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) throws InvalidDataException{
if (speed < 1){
throw new InvalidDataException("negative speed: " + speed);
}
this.speed = speed;
}
public int getVin(){
return vin;
}
public void setVin(int speed){
this.vin = speed;
}
}
这是正确的还是我必须要抓住它呢?