debugjavaMinor
Proper checked exception handling with lambda functions
Viewed 0 times
handlingexceptionwithproperfunctionscheckedlambda
Problem
I'm having trouble finding a clean way to handle checked exceptions thrown by a lambda function passed as a parameter in Java.
I'm writing a Client which will interact via RMI with a Server to play a game; a Client needs to be registered on the Server to be able to login, and it needs to be logged in to play a game. It's a college project, so I have some constraint on what technologies I can use and what I need to do. In particular, when a method raises a
The class I'm focusing on right now will be the interface the client will use to communicate with the server; as of now I have the following structure.
Note: most of the methods I need to implement are still to be written; I'm using a custom exception to remember to implement them.
ServerRMI.java
ServerInterface.java
```
import java.net.InetAddress;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class ServerInterface {
private String nickname;
private InetAddress address;
private int port;
private ServerRMI serverRMI;
public ServerInterface(String nickname, InetAddress address, int port)
throws RemoteException, NotBoundException{
this.nickname = nickname;
this
I'm writing a Client which will interact via RMI with a Server to play a game; a Client needs to be registered on the Server to be able to login, and it needs to be logged in to play a game. It's a college project, so I have some constraint on what technologies I can use and what I need to do. In particular, when a method raises a
RemoteException I've been asked to retry to call the same method a few times. I'm using Java 8 and I wanted to experiment a little bit with lambdas.The class I'm focusing on right now will be the interface the client will use to communicate with the server; as of now I have the following structure.
Note: most of the methods I need to implement are still to be written; I'm using a custom exception to remember to implement them.
ServerRMI.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ServerRMI extends Remote {
public static final String name = "hangman_server_rmi_name";
public int login(String nick, String password)
throws WrongPasswordException,
RemoteException;
public void logout(String nick, int cookie)
throws UserNotLoggedException,
RemoteException;
public void register(String nick, String password)
throws UserAlreadyRegisteredException,
RemoteException;
}ServerInterface.java
```
import java.net.InetAddress;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class ServerInterface {
private String nickname;
private InetAddress address;
private int port;
private ServerRMI serverRMI;
public ServerInterface(String nickname, InetAddress address, int port)
throws RemoteException, NotBoundException{
this.nickname = nickname;
this
Solution
Firstly, a couple of comments:
Why do you have
Now, with regards to your actual question: Yes, you can do exactly that. It's literally that syntax. You can also put multiple
It checks each in order, and subclasses are caught with any statement that matches their superclass.
Now, there are a couple of issues with your approach:
You can fix both problems by either:
-
Use the
Aside from that, I can't see that much wrong with your code. Once I get a chance I'll run it through Eclipse and see if it turns up anything -- if I haven't in 24 hours or so, please reply here and remind me.
ServerInterface is a bad name for something that isn't an interface.Why do you have
//\ before your lambda's body? Java is perfectly content with having it on a new line. As far as I can tell, this is useless.Now, with regards to your actual question: Yes, you can do exactly that. It's literally that syntax. You can also put multiple
catch blocks, so instead of that instanceof mess, you can have this:catch (RemoteException | NotBoundException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Unexpected exception");
}It checks each in order, and subclasses are caught with any statement that matches their superclass.
Now, there are a couple of issues with your approach:
- You're throwing a plain
RuntimeException. This is typically a bad idea, because if you throw a more specific one, people can catch situations where their own exceptional case occurs.
- You don't give any information about what caused the exception, neither in the message nor through the exception itself.
You can fix both problems by either:
- Creating your own custom exception class that carries the information, or
-
Use the
RuntimeException(String, Throwable) constructor, and use a more informative message. That call would look something like:throw new RuntimeException("Unexpected exception: " + e.getMessage(), e);Aside from that, I can't see that much wrong with your code. Once I get a chance I'll run it through Eclipse and see if it turns up anything -- if I haven't in 24 hours or so, please reply here and remind me.
Code Snippets
catch (RemoteException | NotBoundException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Unexpected exception");
}throw new RuntimeException("Unexpected exception: " + e.getMessage(), e);Context
StackExchange Code Review Q#93988, answer score: 3
Revisions (0)
No revisions yet.