meliniak
meliniak

Reputation: 772

java rmi server side exception handling

Do you see any possibility to log server side exceptions?
Consider such code on the server side:

catch (Exception ex) {  
    throw new IllegalStateException (ex);  
}  

The exception is caused by the client-side call. Of course, exception will be noticed on the client-side. Is there any way to somehow handle it on the server side, without catch runtime exceptions? Some kind of handler that would allow me for example to log the stacktrace of the exception?

Any ideas?

Upvotes: 1

Views: 1760

Answers (3)

user207421
user207421

Reputation: 310916

There are RMI system properties that will automatically log server-side exceptions for you. See the links on the RMI Home Page.

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

you can wrap your server instance in a java.lang.reflect.Proxy and implement your server-side logging in the proxy. just make sure the proxy is exported, not the server implementation.

Upvotes: 1

Bohemian
Bohemian

Reputation: 425043

Commonly, the top level server method will have throws Exception.

If you wrap your "do it" code in this method with a try-catch Exception, and you can log it there as still throw it.

public Response myServerTopLevelMethod() throws Exception {
    try {
        myImplCode();
    catch (Exception e) {
        Log.error(e);
        throw e;
    }
}

Now you have some options about what to do. The basic options are:

  1. You can just re-throw the Exception (as above)
  2. You can play nice and return a "something bad happened" response - like many web servers do

Here's an example of option 2:

public Response myServerTopLevelMethod() throws Exception {
    try {
        myImplCode();
    catch (Exception e) {
        Log.error(e);
        return new Response("Yikes! Something exploded... we'll look into it.");
    }
}

Incidentally, the "Log + throw" of option 1 is one of the few times that you ever want to do this; at the top level method. At other times you should generally either log or throw, not both.

Upvotes: 0

Related Questions