2011-10-08 93 views
1

我收到以下錯誤消息在行FileNotFoundException異常 - 表達的非法啓動 - NetBeans的

「表達的llegal開始」拋出FileNotFoundException異常

我做了一些研究,但沒能修理它。你能幫我嗎?非常感謝,z

import java.io.FileNotFoundException; 
    import java.io.File; 
    import java.util.Scanner; 
    import static java.lang.System.out; 

    public class training{ 
     public static void main(String[]args){ 

      throws FileNotFoundException{ 

      Scanner diskScanner = new Scanner(new File("occupancy")); 

      out.println("Room\tGuests"); 

      for(int roomNum = 0; roomNum < 10; roomNum ++){ 
       out.print(roomNum); 
       out.print("\t"); 
       out.println(diskScanner.nextInt()); 
       } 
      } 
     } 
    } 
+0

你到底想幹什麼?聲明主可以拋出異常?或者你在尋找一個try-catch語法? – Corbin

回答

-1

您的語法錯誤。檢查您的來源和/或語言規範。

3

你不應該有一個大括號的拋出關鍵字之前:

public static void main(String[]args) throws FileNotFoundException { 
            ^-- no curly brace 

注意,一類應該始終以大寫字母開頭。

2

不當使用throws

public static void main(String[]args) throws FileNotFoundException 
{ 
.. 
} 

這是更好地使用在try..catch

public static void main(String[]args) 
    { 
     try 
     { 
     .. 
     }catch(FileNotFoundException ex) 
     { 
      // 
     } 
    } 
+0

帶有try/catch塊的代碼的功能基本上是相同的,除了它更長,並且以0(成功)而不是1(錯誤)退出。我會爭論帶有throws子句的代碼更好。 –

+0

@JBNizet同意當前的代碼片斷,但當方法包含其他語句時會發生什麼?在OP後,他/她也必須處理** NoSuchElementException **。 – adatapost

+0

他也可以將這個異常添加到throws子句中。如果只是打印堆棧跟蹤並退出,則不需要捕獲異常。 –

相關問題