我正在尝试向代码中添加一个异常,以便每当securityManager返回false时都将引发异常:
public Appointment(String date,String time) {
//Secure Object Construction
if (securityManager(date, time)) {
this.date=date;
this.time=time;
System.out.println("Valid");
}
//Invalid Object, Throw Error
else {
throw new InstantiationException("Date provided is Invalid");
}
return;
但是,当我添加它时,我出现语法错误:
Unhandled exception type InstantiationException
IDE希望添加该行:
public Appointment(String date,String time) throws InstantiationException
但是,当我这样做时,会触发异常[[总是。
我如何重新格式化代码,以便仅在securityManager返回false时才发生异常?InstantiationException
是已检查的异常。您必须处理它,否则编译器将抱怨此异常。