Client View of Bean  «Prev  Next»

Lesson 7 Exception handling
ObjectiveCatch EJB Exceptions in the Client.

Catch EJB Exceptions in the Client

Reasons exceptions are thrown

Exceptions can be thrown for two major reasons:
  1. There is a problem somewhere between the client and the bean instance (transport, server, container, etc.).
  2. There are application problems in the bean instance itself.

Remote exceptions

In general, the first category will throw either a java.rmi.RemoteException or a javax.ejb.EJBException.
You may think that a RemoteException does not tell you very much. Fortunately, the message (accessible with getMessage()) is initialized with an appropriate description which can be displayed as follows:

View the link below to view RMI Exception Handling.
try { . . . } 
catch (java.rmi.RemoteException e) { 
  System.out.println(e.getMessage()); 
}

A RemoteException may not be fatal. For example, the network could be temporarily congested, and making the same call a moment later may work. A java.rmi.NoSuchObjectException, which is a subclass of RemoteException, will be thrown whenever methods are called on a bean instance that no longer exists.

EJB exceptions

If a system-level exception is detected, a javax.ejb.EJBException will be thrown. This is used to indicate that a fatal problem has been detected. This is an unchecked (sub-class of java.lang.RuntimeException, so it does not need to be caught) exception and the client should abandon any further attempts to access the bean.
Continuing to access a bean instance through either of its home or remote objects after an EJBExcepton has been thrown will not produce any results. An EJBException indicates that a fatal non-recoverable error has occurred.

Application exceptions

The home interface object may throw javax.ejb.CreateException and javax.ejb.RemoveException if it cannot create or remove the bean. These may also be thrown by the bean instance itself if there is a problem with the request, as we shall see later.
The bean may define and throw any exception meaningful to that bean in any of its business methods.
For example:
try {
  String s = bean.sayHello();
  System.out.println(s);
} 
catch (HelloException e) {
  System.out.println(e);
}
catch (RemoteException e) {
  System.out.println(e);
}

In the next lesson, you will be introduced to bean handles.