2012-11-30 46 views
0

我已經從各種帖子中構建了這個類,儘可能做得很好。MySQL - 必須被捕獲或被宣佈引發

我想從MySQL數據庫中獲取用戶名列表。

這裏是方法檢索它們:

public ArrayList<String> LoadUsers() throws SQLException { 
    ArrayList<String> players = new ArrayList<String>(); 
    try { 
     Connection conn = null; 
     ResultSet rs = null; 

     try { 
      conn = getConnection(); 
      Statement s = conn.createStatement(); 
      s.executeQuery ("SELECT * FROM NFT_users"); 
      rs = s.getResultSet(); 

      while(rs.next()){ 
       players.add(rs.getString("name")); 
      } 
      rs.close(); 
      s.close(); 
     } 

     catch (SQLException ex) { 
      System.out.println(ex.toString()); 
     } 

     finally { 
      try { 
       if (conn != null) conn.close(); 
      } 

      catch (SQLException ex) { 
       System.out.println("On close: " + ex.toString()); 
      } 
     } 
    } 

    catch(Exception ex) { 
     //trace(ex); 
    } 

    return players; 
} 

這是我mainclass代碼從方法檢索它:

ArrayList<String> players = database.LoadUsers(); 

但是,我得到的錯誤必須被捕獲或宣佈投擲。我做錯了什麼?

+0

既然你用'try-catch'圍繞你的'LoadUsers()'方法,你不需要拋出異常。你正在處理你的方法中的異常。不要拋出異常或刪除全局'try-catch'。 – jmrodrigg

+0

我自己並沒有寫這麼多。不幸的是,我還不熟悉try-catches。也許你可以發佈一些代碼,這會讓我更容易理解。感謝您的時間! –

回答

3

你的方法: -

public ArrayList<String> LoadUsers() throws SQLException { 

宣佈的SQLException在它的throws子句。因此,無論您使用哪種方法調用此方法,都需要將invocation放在try-catch塊中以處理此異常,或者在該方法的throws子句中也聲明此異常。

因此,假設您從方法caller()調用您的方法。

所以,你有兩個選擇: -

  1. 申報例外的callerthrows條款: -

    public void caller() throws SQLException { 
        ArrayList<String> players = database.LoadUsers(); 
    } 
    
  2. 括在一個try-catch方法調用。在這種情況下,記住要首先聲明你的list外塊:

    public void caller() { 
        ArrayList<String> players = null; 
        try { 
          players = database.LoadUsers(); 
        } catch (SQLException e) { 
          e.printStackTrace(); 
        } 
    } 
    

注意,如果您使用的是第一個選項,那麼你將再次調用caller方法面臨同樣的問題。你也必須遵循這個事情。

通常,在方法內引發的異常是 - 使用try-catch塊在那裏處理,或者將堆棧跟蹤傳播到直接調用方法但不是。你正在用你的方法做這兩件事。您正在處理該例外情況,並已聲明它也被拋出throws子句。你永遠不應該那樣做。

+0

我對Java很新,所以我真的很喜歡一些實際的代碼示例。我迷路了:-) –

+0

@PatrickReck ..你走了。我只是爲這兩個選項添加了示例。 –

+1

另一個註釋 - 按照當前定義,LoadUsers方法實際上並沒有拋出一個SQLException - 它們總是被捕獲,記錄並且不會被重新拋出。這其實是一個衆所周知的反模式。這種方法可能不需要捕捉異常 - 你可以讓調用者處理它們。或者......捕獲並記錄它們,但從方法中刪除'throws'聲明。但不是兩個。 – GreyBeardedGeek

0

試試這個

try{ 
    ArrayList<String> players = database.LoadUsers(); 
}catch(SQLException e){e.printStackTrace();} 

這是因爲你的loadUsers方法拋出異常。所以,無論你將這種方法稱爲何種方法,你都需要用try catch來包圍它。

0

正如我在前面的評論中所提到的那樣,聲明LoadUsers方法拋出一個異常,然後永遠拋出它並不是一個好主意。這引發了調用者應該準備好處理異常的期望,事實上,編譯器會要求調用者用try/catch塊包圍調用,或聲明它拋出異常。

所以,(至少)有三個選項。 第一個是具有LoadUsers方法實際上拋出異常,例如:

public ArrayList<String> LoadUsers() throws SQLException { 
    ArrayList<String> players = new ArrayList<String>(); 
    try { 
     Connection conn = null; 
     ResultSet rs = null; 

     try { 
      conn = getConnection(); 
      Statement s = conn.createStatement(); 
      s.executeQuery ("SELECT * FROM NFT_users"); 
      rs = s.getResultSet(); 

      while(rs.next()){ 
       players.add(rs.getString("name")); 
      } 
      rs.close(); 
      s.close(); 
     } finally { 
      try { 
       if (conn != null) conn.close(); 
      } catch (SQLException ex) { 
       // eat the exception - we can't do anything about it here 
      } 
     } 
    } 

    return players; 
} 

和在呼叫者:

public void processPlayers() { 
    try{ 
     ArrayList<String> players = database.LoadUsers(); 
     // do something with the players 

    } catch(SQLException ex){ 

    ex.printStackTrace(); 
    } 
} 

第二種選擇使用相同LoadUsers()的方法,但沒有按」噸趕上在呼叫者異常要麼 - 它把它送到呼叫者:

public void processPlayers() throws SQLException { 
     ArrayList<String> players = database.LoadUsers(); 
     // do something with the players 
} 

第三個選項是到c在LoadUsers方法中處理異常,而不在調用方中。 這裏的問題是,來電者不會知道有問題 - 它只會得到一個空列表:不建議

public ArrayList<String> LoadUsers() throws SQLException { 
    ArrayList<String> players = new ArrayList<String>(); 
    try { 
     Connection conn = null; 
     ResultSet rs = null; 

     try { 
      conn = getConnection(); 
      Statement s = conn.createStatement(); 
      s.executeQuery ("SELECT * FROM NFT_users"); 
      rs = s.getResultSet(); 

      while(rs.next()){ 
       players.add(rs.getString("name")); 
      } 
      rs.close(); 
      s.close(); 
     } catch(SQLException ex) { 
      ex.printStackTrace(); // but the caller won't know... 
     } 
      finally { 
      try { 
       if (conn != null) conn.close(); 
      } catch (SQLException ex) { 
       // eat the exception - we can't do anything about it here 
      } 
     } 
    } 

    return players; 
} 

public void processPlayers() throws SQLException { 
     ArrayList<String> players = database.LoadUsers(); // an empty list if something went wrong 
     // do something with the players 
} 

最後一個選項。

相關問題