【发布时间】:2016-04-01 02:14:21
【问题描述】:
我不知道如何成功地尝试捕获异常。如您所见,我已经开始了 try-catch 语句,但不知道如何完成它。我收到错误“ tractorException.java:83: error: unreported exception tractorException; 必须被捕获或声明为抛出 setVehicleID(0); "
import java.io.*;
import java.util.*;
import javax.swing.*;
public class tractorException extends Exception {
protected int VehicleID;
public int setVehicleID(int VehicleID) throws tractorException {
if (VehicleID <= 0 || VehicleID > 100000) {
throw new tractorException();
} else {
this.VehicleID = VehicleID;
return this.VehicleID;
}
}
public int getVehicleID() {
return this.VehicleID;
}
tractorException() {
setVehicleID(0);
}
public static void main (String[] args) {
try {
throw new Exception("Something went wrong!!");
} catch (Exception e) {
}
【问题讨论】:
-
你想达到什么目的?一个无限抛出异常的类?
-
是的,我正在尝试实现一个只抛出异常的类