【发布时间】:2013-06-03 03:19:52
【问题描述】:
try/catch 的范围是什么?本质上,我正在反序列化一些对象并创建新的引用来存储它们。加载它们后,我尝试在引用中使用一种方法,但出现以下编译错误。
try{
ObjectInputStream is = new ObjectInputStream(new FileInputStream("saveGame.ser"));
gameCharacter oneRestore = (gameCharacter) is.readObject();
gameCharacter twoRestore = (gameCharacter) is.readObject();
gameCharacter threeRestore = (gameCharacter) is.readObject();
} catch (Exception ex) {ex.printStackTrace();}
System.out.println("One's type is: " + oneRestore.getType());
System.out.println("Two's type is: " + twoRestore.getType());
System.out.println("Three's type is: " + threeRestore.getType());
编译错误是:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
oneRestore cannot be resolved
twoRestore cannot be resolved
threeRestore cannot be resolved
【问题讨论】:
-
与任何其他代码块一样,它的范围从
{大括号开始,到}大括号结束(类似于if-else或for循环块)。请注意,编译器错误对此非常清楚。 -
我知道错误是什么,以及如何解决它。但是,我没有意识到非常明显的范围 = {->