该程序用于异常处理,但问题出在UserException ????
import org.omg.CORBA.UserException;
public class Ch9_3_3 {
class UserException extends Exception {
int data;
public UserException(int d) { data = d; }
public String getMessage() {
return ("Error! You negotiate too much for auction price: " + data);
}
}
public static void main(String[] args) {
try {
for ( int i = 0; i < 5; i++) {
if (i==3) throw new UserException(3);
System.out.println("No. of auction: " + i);
}
}
catch ( UserException ex) {
System.out.println("Exception note: " + ex.getMessage());
System.out.println("Exception reason: ");
ex.printStackTrace();
return;
}
finally { System.out.println("Error handling completed."); }
System.out.println("End of program!");
}
}
错误消息:线程“主”中的异常java.lang.Error:未解决的编译问题:无法访问类型为Ch9_3_3的封闭实例。必须使用类型为Ch9_3_3的封闭实例(例如xx是Ch9_3_3的实例)来限制分配。在Ch9_3_3.main(Ch9_3_3.java:16)
注意:第16行是 try {
您的代码下面有一个编译错误
if (i==3) throw new UserException(3);
您不能像这样创建内部类的实例。相反,您可以
使内部类静态。因为您是从主要班级调用它的。
public static class UserException extends Exception
或可以使对象如下
if (i==3) throw new Ch9_3_3().new UserException(3);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句