2013-11-15 166 views
-2

這是發生這種事的堰,因爲就我所見,一切都是正確的。如果你能幫助我,我會感謝 下面的代碼:爲什麼我得到這個「未報告的異常」?

import java.io.*; 
import java.net.*; 
import java.util.*; 
import javax.swing.*; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.ResultSet; 
import java.sql.Statement; 

public class Server{ 

private static HashSet<String> users = new HashSet<String>(); 
//private static HashSet<String> passwords = new HashSet<String>(); 
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>(); 
private static final int port = 9001; 

private static class SocketHandler extends Thread{ 

    PrintWriter output; 
    BufferedReader input; 
    String name; 
    //String pass; 
    Socket socket; 

    public SocketHandler(Socket sc){ 
     this.socket = sc; 
    } 

    public void run(){ 

    input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     output = new PrintWriter(socket.getOutputStream(), true); 

     try{ 

      while(true){ 

       output.println("SUBMITNAME"); 
       name = input.readLine(); 
       if(name == null){ 
        return; 
       } 
       synchronized(users){ 
        if(!users.contains(name)){ 
         users.add(name); 
         break; 
        } 
       } 
      } 

      output.println("NAMEACCEPTED"); 
      writers.add(output); 

      while(true){ 

       String in = input.readLine(); 
       if(in == null){ 
        return; 
       } 
       for(PrintWriter writer : writers){ 
        writer.println("MESSAGE: " + name + ": " + in); 
       } 
      } 

     }catch(IOException e){ 
      System.out.println(e); 
     }finally{ 

      if(name != null){ 
       users.remove(name); 
      } 
      if(output != null){ 
       writers.remove(output); 
      } 
      try{ 
       socket.close(); 

      }catch(IOException e){ 
       System.out.println(e); 
      } 

     } 
    } 
} 

public static void main(String[]args) throws Exception{ 

    ServerSocket listener = new ServerSocket(port); 
    System.out.println("The server is now running..."); 

    try{ 

     while(true){ 
      new SocketHandler(listener.accept()).start(); 
     } 

    }catch(IOException e){ 
     System.out.println(e); 
    }finally{ 
     listener.close(); 
    } 

    Connection con = null; 
    ResultSet rs = null; 
    Statement s = null; 

    try{ 

     Class.forName("org.sqlite.JDBC"); 
con = DriverManager.getConnection("jdbc:sqlite:/home/julio/Documents/Prueba.db"); 
     s = con.createStatement(); 
     rs = s.executeQuery("E-MAIL DATABASE"); 
     while(rs.next()){ 

    System.out.println("Enter your email adress: " + rs.getString("name")); 

     } 

    }catch(Exception e){ 
     e.printStackTrace(); 
    }finally{ 
     try{ 
      rs.close(); 
      s.close(); 
      con.close(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

}

我不知道爲什麼會出現這種情況,如果你可以在代碼中看到的,我扔了,並抓住每一個異常..

This is the error that displays on screen

這是錯誤的屏幕顯示

+2

'IOException異常:必須捕獲或宣佈爲thrown.'還有什麼不明白嗎?在'try'塊中移動你的語句。 –

+0

可能的重複[Unreported exception java.sql.SQLException;必須被捕獲或聲明爲拋出?](http://stackoverflow.com/questions/4457352/unreported-exception-java-sql-sqlexception-must-be-caught-or-declared-to-be-thr) –

回答

1

檢查兩條線,其中這些埃羅rs發生:它們在try塊之外。

input = new BufferedReader(new InputStreamReader(socket.getInputStream())); // error 1: not in the try block! 
output = new PrintWriter(socket.getOutputStream(), true); // error 2: not in the try block! 

try { 

    // ... other code ... 

} catch(IOException e) { 
    System.out.println(e); 
} finally { 

    if (name != null) { 
     users.remove(name); 
    } 
    if (output != null) { 
     writers.remove(output); 
    } 
    try { 
     socket.close(); 

    } catch (IOException e) { 
     System.out.println(e); 
    } 

} 

您應該將作業放在try區塊的內部。

0

在try塊內部分配輸入和輸出。

public void run(){ 
    try{ 
     input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     output = new PrintWriter(socket.getOutputStream(), true); 

     .... 

} 

}

+0

類字段默認初始化爲「空」(如果它們是對象引用)。如果它們是局部變量,則只需要初始化它們。 –

+0

@RobinKrahl謝謝你,你是對的。我已經糾正了答案。 – holap

相關問題