一般Error类是不自己创建,错误就是错误了,这是修改代码都无法解决的事情。
而Exception类是可以做自己的异常类的。
首先看代码
package endual;
public class CarWrongException extends Exception{
public CarWrongException(){
}
public CarWrongException(String msg) {
super(msg) ;
}
}
然后
package endual;
public class Car {
public static final int OK = 1 ;
public static final int Worker = 2 ;
public int run(boolean isTrue) throws CarWrongException{
if (true) {
return 1 ;
}
else {
throw new CarWrongException("车子不能不动的") ;
}
//return 1 ;
}
}