-2

It's weir that this is happening, because as I see it, everything's correct. If you can help me, I'll be thankful Here's the code:

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();
        }
    }
}

}

I don't know why is this happen if as you can see on the code, I throw, and catch every Exception..

This is the error that displays on screen

This is the error that the screen displays

  • 2
    `IOException: must be caught or declared to be thrown.` What don't you understand? Move your statement inside the `try` block. – Sotirios Delimanolis Nov 15 '13 at 15:37
  • possible duplicate of [Unreported exception java.sql.SQLException; must be caught or declared to be thrown?](http://stackoverflow.com/questions/4457352/unreported-exception-java-sql-sqlexception-must-be-caught-or-declared-to-be-thr) – Ernest Friedman-Hill Nov 15 '13 at 15:39

2 Answers2

1

Check the two lines where these errors occur: They are outside the try block.

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);
    }

}

You should put the assignments inside of the tryblock.

Robin Krahl
  • 5,268
  • 19
  • 32
0

assign input and output inside the try block.

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

        ....

}

}

holap
  • 438
  • 2
  • 18
  • Class fields are initialized to `null` (if they are object references) by default. You would only have to initialize them if they were local variables. – Robin Krahl Nov 15 '13 at 15:45