2016-03-01 36 views
1

在這個例子中,爲什麼使用空的「返回」(在catch塊中)? (對不起,我的英語) 提前謝謝!在這個例子中,爲什麼使用空迴歸?

public class DeserializeDemo 
{ 
    public static void main(String [] args) 
    { 
     Employee employee; 
     try 
     { 
      ObjectInputStream in = new ObjectInputStream(new FileInputStream("E:\\serialization\\employee.ser")); 
      employee = (Employee) in.readObject(); 
      in.close(); 
     }catch(IOException i) 
     { 
      i.printStackTrace(); 
      return; //this one 
     }catch(ClassNotFoundException c) 
     { 
      System.out.println("Employee class not found"); 
      c.printStackTrace(); 
      return; //...and this one! 
     } 
     System.out.println("Deserialized Employee..."); 
     System.out.println("Name: " + employee.name); 
    } 
} 
+1

可能重複[在Java中的void方法中返回關鍵字做什麼?](http://stackoverflow.com/questions/744676/what-does-the-return-keyword-do-in-a- void-method-in-java) –

回答

3

return,如名稱所示,退出當前方法並返回給調用者。在你的例子中,如果發生錯誤,它會返回而不寫最後的消息,因爲它不能反序列化員工。

+0

非常感謝! –

-1

我從另一個線程

複製此Java語言規範說,你可以有一個不帶表情的回報,如果你的方法返回void。它的作用與具有指定參數的函數的返回值相同,除非它不返回任何內容,因爲沒有任何內容可以返回,控制權將傳回給調用方法。

相關問題