【发布时间】:2017-05-15 09:49:10
【问题描述】:
在使用 java 编码时,我看到发生了一件奇怪的事情。
代码是这样写的。
class lara {
public static void main(String ...pro) {
int o;
try{
o=999;
}catch(Exception mu) {
System.out.println("sololobo");
}
System.out.println(o);
}
}
它会在要求打印 o 的那一行显示错误
"System.out.println(o);"
但是当我添加类型“return;”时catch 块中的语句,如
class lara {
public static void main(String ...pro) {
int o;
try{
o=999;
}catch(Exception mu){
System.out.println("sololobo");
return;
}
System.out.println(o);
}
}
效果很好。
为什么会这样?
是catch函数吗?
这个返回语句指的是什么函数。
提前谢谢你!
【问题讨论】:
-
请花点时间将您的代码格式化更加更易读,并使用适当的缩进和换行符。如果您使用的是 IDE,它可能会对您有所帮助。你没有告诉我们错误是什么也无济于事......
-
当您添加 return 语句时,它会从该函数返回,在您的情况下为
main函数,因此之后的代码很简单无法访问。