Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT Throwing exception to client

Can someone show to throw exception to client in GWT.

in my serviceasync interface i am doing this as well in my service interface

void ActivateUserAccount(String ActivationCode,AsyncCallback <Boolean> Callback) throws AlreadyActivatedError;

in my serverimpl;

i am doing this to throw an exception

public Boolean ActivateUserAccount(String ActivationCode) throws AlreadyActivatedError
    {
....
throw new AlreadyActivatedError();
}

my exception is in the form:

public class AlreadyActivatedError extends Exception implements IsSerializable
{
    public AlreadyActivatedError()
    {
        super();
    }
}
like image 205
Noor Avatar asked Jan 28 '11 06:01

Noor


People also ask

How do you handle throwing exceptions?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.

What causes an exception to be thrown?

Exceptions are used to indicate that an error has occurred while running the program. Exception objects that describe an error are created and then thrown with the throw keyword.

Is it possible to reach throw an exception?

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects). If you re-throw the exception, just like in the case of throws clause this exception now, will be generated at in the method that calls the current one.

What does throwing an exception mean?

The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.


1 Answers

Just to make everything clear: you can throw both checked (the ones extending Exception) and unchecked (extending RuntimeException) exceptions from the server to the client - as long as the exception is serializable. It is however recommended to throw checked exceptions, as they

represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files).

In contrast, unchecked exceptions

represent defects in the program (bugs) - often invalid arguments passed to a non-private method.

Source

As the documentation states, the following conditions have to be fulfilled for an exception to be sent to the client:

  • It has to extend Exception (note that RuntimeException does that).
  • It has to be serializable. In short: implement Serializable, have a no-args constructor and have all the fields serializable.
  • In your *Service interface you need to add a throws declaration to the method that can throw the exception. Note that you don't need to add the throws declaration to the *Async interface.

Once you have that set up, you'll be able to handle the exception in the onFailure method in your AsyncCallback.

Some code to show all the pieces together, based on examples from the guide on the GWT site:

DelistedException.java

public class DelistedException extends Exception implements Serializable {

  private String symbol;

  // Note the no-args constructor
  // It can be protected so that only subclasses could use it
  // (because if they want to be serializable too, they'll need
  // a no-args constructor that calls the superclasses' no-args constructor...)
  protected DelistedException() {
  }

  public DelistedException(String symbol) {
    this.symbol = symbol;
  }

  public String getSymbol() {
    return this.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);
}

On the client side

AsyncCallback<StockPrice[]> callback = new AsyncCallback<StockPrice[]>() {
  public void onFailure(Throwable caught) {
    if (caught instanceof DelistedException) {
      // Do something with it
    } else {
      // Probably some unchecked exception,
      // show some generic error message
  }

  public void onSuccess(StockPrice[] result) {
    // Success!
  }
};

stockPriceService.getPrices(symbols, callback);
like image 63
Igor Klimer Avatar answered Oct 21 '22 05:10

Igor Klimer