try {
Man Fred = new Man();
} catch (exception e) {
Print e
}
Print Fred.getName ();
弗雷德现在不在范围之内。我应该怎么做?
最佳答案
您似乎知道Java语法,所以在伪代码中
Man Fred = null;
try {
Fred = new Man();
} catch (exception e) {
print e
} finally {
//You could do some clean up.
}
if(Fred != null) {
print Fred.getName();
}
如果不..
Man fred = null;
try {
fred = new Man();
} catch (Exception e) {
System.out.println(e.printStackTrace());
} finally {
//You could do some clean up.
}
if(fred != null) {
System.out.println(fred.getName());
}
必须在try块之外声明Fred变量。因此,它在方法或外部范围中都是可见的。
关于java - 使用try语句声明对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13025448/