Reputation: 65
I'm working on a Client - Server application with Java Sockets.
The client must wait for a response from the server. When the server receives the client's request, it performs certain tasks to obtain a result that must be passed to the client - but performing these tasks could cause an error.
Is there any way in which when this error occurs the application does not terminate and can continue processing requests?
package Socket;
import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server
{
public static void main(String[] args)
{
MainWindow application=new MainWindow();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MainWindow extends JFrame implements Runnable
{
public MainWindow()
{
setBounds(1200,300,280,350);
JPanel myPanel= new JPanel();
myPanel.setLayout(new BorderLayout());
text=new JTextArea();
myPanel.add(text,BorderLayout.CENTER);
add(myPanel);
setVisible(true);
Thread t= new Thread(this);
t.start();
}
private JTextArea text;
@Override
public void run()
{
try
{
ServerSocket Server = new ServerSocket(9999);
while(true)
{
Socket mySocket = Server.accept();
DataInputStream input_stream = new DataInputStream(mySocket.getInputStream());
String message = input_stream.readUTF();
someMethod(); //This method can generate an exception, then the application closes and I can not receive new requests
text.append("\n" + message);
mySocket.close();
}
}
catch (IOException ex)
{
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Upvotes: 0
Views: 75
Reputation: 8589
Catch the exception inside the loop.
ServerSocket Server = new ServerSocket(9999);
while(true) {
try {
Socket mySocket = Server.accept();
DataInputStream input_stream = new DataInputStream(mySocket.getInputStream());
String message = input_stream.readUTF();
someMethod(); //This method can generate an exception, then the application closes and I can not receive new requests
text.append("\n" + message);
mySocket.close();
} catch (IOException ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
Then you'll log the exception and just carry on.
PS: There are probably some exceptions that mean that retries are futile and you need to abandon the process rather than fill the log with a zillion severe errors.
I recommend reading the documentation and devising a slightly more mature error handling strategy than 'ignore everything and soldier on'.
Upvotes: 5
Reputation: 114320
A try-catch does not have to go around all your code at once. The one you have around your whole run
method is very useful to log errors that are truly fatal. You can have another one around someMethod();
to prevent those exceptions from breaking the loop:
try {
someMethod();
} catch(ZeroDivisionException e) {
// handle the exception
} catch(SomeOtherException e) {
// handle the other exception
}
The exceptions here are made up, but I'm sure you get the idea. How you handle them is up to you. As long as you don't re-throw any of them, the loop won't end and your outer block won't be triggered.
You can nest catch blocks pretty much however you like, so you can catch IOException
in the inner block as well as the outer one. The outer block will still clean up for the errors you can't control.
Upvotes: 2