title: User Defined Exceptions with JMX
body: <p>I have exposed methods for remote management in my application server using JMX by creating an MXBean interface, and a class to implement it. Included in this interface are operations for setting attributes on my server, and for getting the current value of attributes. For example, take the following methods:</p>
  
  <pre><code>public interface WordManagerMXBean {
       public void addWord(String word);
       public WordsObject getWords();
       public void removeWord(String word);
  }
  </code></pre>
  
  <p>The WordsObject is a custom, serializable class used to retrieve data about the state of the server. Then I also have a WordManager class that implements the above interface. I then create a JMX agent to manage my resource:</p>
  
  <pre><code>MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  ObjectName wordManagerName = new ObjectName("com.example:type=WordManager");
  mbs.registerMBean(wordManager, wordManagerName);
  </code></pre>
  
  <p>I have created a client that invokes these methods, and this works as expected. However, I would like to extend this current configuration by adding user defined exceptions that can be sent back to my client. So I would like to change my interface to something like this:</p>
  
  <pre><code>public interface WordManagerMXBean {
       public void addWord(String word) throws WordAlreadyExistsException;
       public WordsObject getWords();
       public void removeWord(String word);
  }
  </code></pre>
  
  <p>My WordAlreadyExistsException looks like this:</p>
  
  <pre><code>public class WordAlreadyExistsException extends Exception implements Serializable {
  
  private static final long serialVersionUID = -9095552123119275304L;
  
  public WordAlreadyExistsException() {
      super();
      }
  }
  </code></pre>
  
  <p>When I call the addWord() method in my client, I would like to get back a WordAlreadyExistsException if the word already exists. However, when I do this, I get an error like this:</p>
  
  <pre><code>java.rmi.UnmarshalException: Error unmarshaling return; nested exception is: 
  java.lang.ClassNotFoundException: com.example.WordAlreadyExistsException
  </code></pre>
  
  <p>The WordAlreadyExistsException, the WordsObject and the WordManagerMXBean interface are all in a single jar file that is available to both the client and the server. If I call the getWords() method, the client has no difficulty handling the WordsObject. However, if a user defined exception, like the one above, is thrown, then the client gives the error shown above. Is it possible to configure JMX to handle this exception correctly in the client?</p>
  
  <p>Following some searching, I noticed that there is an MBeanException class that is used to wrap exceptions. I'm not sure if this wrapping is performed by the agent automatically, or if I'm supposed to do the wrapping myself. I tried both, but in either case I get the same error on the client.</p>
  
  <p>I have also tried this with both checked and unchecked exceptions, again the same error occurs.</p>
  
  <p>One solution to this is to simply pass back the error string inside a generic error, as all of the standard java exceptions work. But I'd prefer to get back the actual exception for processing by the client. </p>
  
  <p>Is it possible to handle user defined exceptions in JMX? If so, any ideas how?</p>
  
  <p>EDIT: Including full stack trace</p>
  
  <pre><code>java.lang.reflect.UndeclaredThrowableException
  at $Proxy4.addWord(Unknown Source)
  at com.example.TestClient.addWord(TestClient.java:76)
  at com.example.TestClient.execute(TestClient.java:56)
  at java.lang.Thread.run(Thread.java:722
  Caused by: java.rmi.UnmarshalException: Error unmarshaling return; nested exception is: 
  java.lang.ClassNotFoundException: com.example.WordAlreadyExistsException
  at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:245)
  at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
  at com.sun.jmx.remote.internal.PRef.invoke(Unknown Source)
  at javax.management.remote.rmi.RMIConnectionImpl_Stub.invoke(Unknown Source)
  at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.invoke(RMIConnector.java:1017)
  at  javax.management.MBeanServerInvocationHandler.invoke(MBeanServerInvocationHandler.java:305)
  ... 6 more
  Caused by: java.lang.ClassNotFoundException: com.example.WordAlreadyExistsException
  at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Class.java:264)
  at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:453)
  at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:184)
  at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637)
  at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:264)
  at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:216)
  at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
  at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
  at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
  at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
  at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
  at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
  at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
  at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
  at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:243)
  ... 11 more
  </code></pre>