日期:2014-05-20  浏览次数:20628 次

下面这段代码中的return语句有什么用?
class Grumpy extends Exception{}
class TooHot extends Grumpy{}
class TooTired extends Grumpy{}

int temp,sleep;
temp=50;
sleep=4;
try{
if (temp>40)
throw (new TooHot());
if (sleep<8)
throw (new TooTired());
}
catch(Grumpy g){
if (g instanceof TooHot){
System.out.println("caught too hot.");
return;
}
if (g instanceof TooTired){
System.out.println("caught too tired.");
return;
}
}
finally{
System.out.println("in the finally clauses.");
}


------解决方案--------------------
虽然有没有return执行结果是一样的.但有return,效率会更高.如果g instanceof TooHot,有return就不会去执行后一个if.