Just to make everything clear: you can throw both checked (those that extend Exception ) and unchecked (those that extend RuntimeException ) exceptions from server to client β as long as the exception is serializable . However, it is recommended that you throw checked exceptions as they are
constitute unacceptable conditions in areas beyond the direct control of the program (invalid user input, database problems, network failures, missing files).
On the contrary, excluded exceptions
represent defects in the program (errors) - often invalid arguments are passed to a non-private method.
A source
As stated in the documentation , the following conditions must be met to send an exception to the client:
- It should extend
Exception (note that RuntimeException does this). - Must be serializable . In short:
Serializable implementation, have no-args constructor and have all fields serializable. - In your
*Service interface, you need to add a throws declaration to a method that may throw an exception. Please note that you do not need to add a throws declaration to the *Async interface.
After you configure this setting, you can handle the exception in the onFailure method of AsyncCallback .
Some code to show all parts together, based on examples from the manual on the GWT website :
DelistedException.java
public class DelistedException extends Exception implements Serializable { private String symbol;
StockPriceService.java
@RemoteServiceRelativePath("stockPrices") public interface StockPriceService extends RemoteService { StockPrice[] getPrices(String[] symbols) throws DelistedException; }
StockPriceServiceAsync.java
public interface StockPriceServiceAsync { void getPrices(String[] symbols, AsyncCallback<StockPrice[]> callback); }
Client side
AsyncCallback<StockPrice[]> callback = new AsyncCallback<StockPrice[]>() { public void onFailure(Throwable caught) { if (caught instanceof DelistedException) {
Igor Klimer
source share