1

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?

meliniak
  • 762
  • 1
  • 9
  • 16

3 Answers3

1

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.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
0

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • i intended to do this without try-catching body of every rmi method, but this seems to be the most efficient and easiest way to log exceptions. thanks. – meliniak Jun 10 '11 at 14:38
  • -1. The top level server method can only be declared to throw the exceptions specified in the remote interface. Unless these are equally sloppily defined this suggestion won't even compile. – user207421 Jul 11 '11 at 04:24
  • I'm not suggesting this is a *good* idea. I'm just saying that is what you typically have to work with - you can't change it, you just have to live with an interface that requires you to declare `throws Exception`. That is the premise for my answer. – Bohemian Jul 11 '11 at 05:32
  • Combine this with one of the answers of [this question](http://stackoverflow.com/questions/8657941/java-annotation-for-wrapping-a-method) in order to avoid try/cath block in every method... – Pierre Henry Feb 12 '15 at 15:05
0

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I'm calling a method on the server through rmi. I throw a runtime exception in this method and have set both _sun.rmi.server.exceptionTrace_ and _java.rmi.server.logCalls_ to true but I don't get any stacktrace in the server log. Any ideas why? – T3rm1 Aug 21 '12 at 15:22
  • FYI, setting `-Dsun.rmi.server.exceptionTrace=true` on Glassfish 3.1.2.2 worked for me. I compared the behaviour before and after: before the client received the exception but it did not appear in the server logs, after the client received the exception _and_ it appeared in the server logs. The client is an "Enterprise Application Client" invoking an EJB. The EJB method just throws an `IllegalArgumentException`. – DavidS Aug 10 '15 at 18:31
  • @DavidS It's not clear what you were expecting here. As the name suggests, it traces the exception. It doesn't have any effect *on* the exception. – user207421 Aug 10 '15 at 21:57
  • To clarify, I was just commenting so that future readers know that your suggestion works, even though T3rm1 had trouble implementing it. (I would have addressed T3rm1 directly, but as his comment is three years old, I assume he's moved on, so I didn't want to bother him.) – DavidS Aug 10 '15 at 22:03