Exception handling in GWT Service

1,689 views
Skip to first unread message

satya

unread,
Nov 23, 2008, 12:58:37 AM11/23/08
to Google Web Toolkit, satya...@gmail.com
Hi,

Can i pass exceptions between client and server.
Can i have an RPC serice with method that throw an user defined
exception and handle that exception in the client side?


I created an userdefined exception:
------------------------------------------------------
public class IllegalDateRangeException extends SerializationException
implements IsSerializable
{
private String errorMessage = null;

public IllegalDateRangeException ( String error )
{
errorMessage = error;
}

public String toString()
{
return "Exception occurred: " + errorMessage;
}

public String getErrorMessage()
{
return errorMessage;
}

public void setErrorMessage( String errorMessage )
{
this.errorMessage = errorMessage;
}

}


and my RPC method throws this exception:
------------------------------------------------
public String create(Rule rule) throws IllegalDateRangeException;

and i want to catch this exception in the client side and perform
actions.

But i get the following error from GWT:
[ERROR] Type
'com.cerner.cwx.ruleswizard.client.IllegalDateRangeException' was not
serializable and has no concrete serializable subtypes

Any suggestions?
Any help on this is greatly appreciated.

Thank you
Satya

olivier nouguier

unread,
Nov 23, 2008, 6:23:50 AM11/23/08
to Google-We...@googlegroups.com
On Sun, Nov 23, 2008 at 6:58 AM, satya <satya...@gmail.com> wrote:
>
> Hi,
>
> Can i pass exceptions between client and server.
> Can i have an RPC serice with method that throw an user defined
> exception and handle that exception in the client side?
Yes as long as your exeption is declared in the Service Signature (to
be declared as "GWT serialisable").
--
Si l'ignorance peut servir de consolation, elle n'en est pas moins illusoire.

Litty Preeth

unread,
Nov 23, 2008, 11:55:06 AM11/23/08
to Google-We...@googlegroups.com
I think you shud hav a no argument constructor for ur exception tooo....

satya

unread,
Nov 25, 2008, 2:10:03 PM11/25/08
to Google Web Toolkit
How do i specify in the service signature that this is GWT
serializable?

my method signature looks like:
public String create(Rule rule) throws IllegalDateRangeException;

On Nov 23, 5:23 am, "olivier nouguier" <olivier.nougu...@gmail.com>
wrote:

satya

unread,
Nov 25, 2008, 2:14:37 PM11/25/08
to Google Web Toolkit
Also

when i call the actual method, does the exception get caught in the
failure method or in my catch method?


For example:


AsyncCallback callBack = new AsyncCallback() {
public void onFailure( Throwable caught )
{
//Is the exception caught here?????
}

public void onSuccess( Object result )
{

}
try
{
createNewRuleService.createRule( screenType,
eventRuleForm, callBack );
}
catch ( IllegalDateRangeException e )
{
//// or the exception is caught here?

}


Thank you very much for your help. I appreciate it.

Satya

olivier nouguier

unread,
Nov 25, 2008, 2:38:14 PM11/25/08
to Google-We...@googlegroups.com
On Tue, Nov 25, 2008 at 8:14 PM, satya <satya...@gmail.com> wrote:
>
> Also
>
> when i call the actual method, does the exception get caught in the
> failure method or in my catch method?
>
>
> For example:
>
>
> AsyncCallback callBack = new AsyncCallback() {
> public void onFailure( Throwable caught )
> {
> //Is the exception caught here?????
here !
> }
>
> public void onSuccess( Object result )
> {
>
> }
> try
> {
> createNewRuleService.createRule( screenType,
> eventRuleForm, callBack );
> }
> catch ( IllegalDateRangeException e )
> {
> //// or the exception is caught here?
>
> }
>
You should notice the differences between RemoteInterface and its
Asyn counter part:
* return type: the asyn interface method doesn't have return value
===> it will be pass as parameter asynchronously in onSuccess
callback.
* exception: are not part of the async method ===> it will be pass
as parameter asynchronously in onFailure callback.

satya

unread,
Nov 25, 2008, 3:08:06 PM11/25/08
to Google Web Toolkit
Thank you very much,
I guess i can return the String "IllegalDateRangeException" when
throwing that exception from my Service and check for that in my
onFailure class.

public void onFailure( Throwable caught )
{
if ( caught.getMessage().contains
( "IllegalDateRangeException" ) )

//handle my class.
}

Thnak you,
Satya

On Nov 25, 1:38 pm, "olivier nouguier" <olivier.nougu...@gmail.com>
wrote:

Ravi M

unread,
Nov 25, 2008, 3:32:42 PM11/25/08
to Google Web Toolkit
Satya,

The following should work.

1. Declare your RPC exception like so:

public class MyRPCException extends SerializableException {
public TrackerRPCException() {
super();
}

public MyRPCException(String message) {
super(message);
}

//... other stuff?
}

2. Declare your service method like so:

public SomeReturnedObject myServiceMethod(...arguments...) throws
MyRPCException;

3. Declare your async service method like so:

public void myServiceMethod(...arguments..., AsyncCallback callback);

4. The server side implementation of your service method may be like
so:

public SomeReturnedObject myServiceMethod(...arguments...) throws
MyRPCException {
SomeReturnedObject ret = null;
try {
// Do stuff to "populate" ret. This stuff could
potentially fail.
} catch (SomeNonGWTSerializableServerSideException e) {
throw new MyRPCException("This is an error message that
may be appropriate to show in the UI: " + e.getMessage());
}
return ret;
}

5. In your AsyncCallback in the client:

public void onSuccess(Object result) {
SomeReturnedObject foo = (SomeReturnedObject) result;
// Happiness and tranquility reign supreme
};

public void onFailure(Throwable exception) {
try {
throw exception;
} catch (MyRPCException e) {
// Show the error message to the user,
// or handle however you want to.
}
};

This should work, there may be some overkill in this though. All this
is GWT 1.4.6x, I'm not sure if any of this has changed in 1.5.x.

Hope this helps

Ravi

Ravi M

unread,
Nov 25, 2008, 3:33:43 PM11/25/08
to Google Web Toolkit
Ah me. Step 1 should read:

1. Declare your RPC exception like so:

public class MyRPCException extends SerializableException {
public MyRPCException() {

jossey

unread,
Nov 26, 2008, 7:58:33 AM11/26/08
to Google Web Toolkit
Hi,
Why are we extending SerializableException?
Is that because the starting example had it so?
I think it kinds of gives out a wrong message. ...
just extends Exception should do.

not very relevant to the discussion.... anyways...


Jossey.

Ravi M

unread,
Nov 26, 2008, 8:56:54 AM11/26/08
to Google Web Toolkit
Jossey

Quite the contrary, actually. I started off with a custom exception
which was merely a child class of Exception that I intended to use in
RPC calls (i.e. service methods throw it). This actually didn't work
in the ways expected, i.e. when the server threw the exception I was
unable to retrieve the exception message in the client, or find out
what the cause was. Which is when some digging around revealed that if
you want to send the exception over the wire, it has to be a GWT
SerializableException.

This is with 1.4.6x, of course this may have changed in 1.5.x, I am
not sure.

Ravi

jossey

unread,
Nov 26, 2008, 1:54:25 PM11/26/08
to Google Web Toolkit
I am sorry... my bad... I read it as SerializationException....
Looked at the SerializableException in gwt... it is just an Exception
implementing IsSerializable interface... so it all makes sense.
Thanks Ravi for clarifying.

BTW, in 1.5 gwt supports serialization of objects implementing
java.io.Serializable.
So just by extending Exception it would work.

Thanks again,
Jossey.

satya

unread,
Nov 26, 2008, 11:08:15 PM11/26/08
to Google Web Toolkit
There is a SerializationException

(public class SerializationException
extends java.lang.Exception) in gwt1.5.

SerializableException is depricated in 1.5

I shall try your suggestion of extending the Exception or use the
SerilizationException and post the code here.

Thank you both for your help,
Appreciate it
Satya

Suri

unread,
Dec 24, 2008, 11:23:35 AM12/24/08
to Google Web Toolkit
Hey all,
I was looking for a solution to the exception handling described above
and tried the technique suggested. However, when I implement the
exception in the onFailure method as shown, eclipse barfs and shows a
"unhandled exception type throwable" error at me. Same thing happens
when I try to compile anyway.

- I'm using GWT 1.5 and tried extending the exception with both
Exception as well as SerializationException
- Also, I'm implementing a fascade pattern as described in the GWT in
Action Book for the RPC calls.

Here's a scenario of what I'm trying

public class MyRPCException extends Exception
{
// same definition of exception as above really.
}

public interface MyRPCService
{
ArrayList<SomeType> load() throws MyRPCException;
}

public interface MyRPCServiceAsync
{
void load(AsyncCallback<ArrayList<SomeType>>);
}

public class MyRPCFascade
{
private MyRPCFascade() // singleton is used just didn't
bother inserting code here because i think its irrelevant to the
issue.
{
proxy = (MyRPCServiceAsync) GWT.create(MyRPCService.class);
ServiceDefTarget endPoint = (ServiceDefTarget) proxy;
endPoint.setServiceEntryPoint("/myRPCService");
}

public void load(AsyncCallback<ArrayList<SomeType>>)
{
proxy.load(callback);
}
}

--- Server side ----
public class MyRPCServiceImpl implements MyRPCService
{
public ArrayList<SomeType> load() throws MyRPCExecption
{
try {.....}
catch(SomeException e) { throw new MyRPCException(); }
}
}

----- Client Side ----
myRPCFascade.load(new SomeTypeCallback() )


class SomeTypeCallback implements AysncCallback<ArrayList<SomeType>>
{
public void onSuccess(ArrayList<SomeType> result)
{
// .... process result
}

public void onFailure(Throwable caught)
{
try
{
throw caught; // **** This is where
compilation Fails
}
catch(MyRPCException e)
{
Window.alert("DING");
}
}
}

Any ideas what I'm doing wrong?


Thanks

Suri

On Nov 26, 11:08 pm, satya <satya.mu...@gmail.com> wrote:
> There is a SerializationException
>
> (public class SerializationException
> extends java.lang.Exception)   in gwt1.5.
>
> SerializableException is depricated in 1.5
>
> I shall try your suggestion of extending theExceptionor use the
> SerilizationException and post the code here.
>
> Thank you both for your help,
> Appreciate it
> Satya
>
> On Nov 26, 12:54 pm, jossey <joss...@gmail.com> wrote:
>
> > I am sorry... my bad... I read it as SerializationException....
> > Looked at the SerializableException in gwt... it is just anException
> > implementing IsSerializable interface... so it all makes sense.
> > Thanks Ravi for clarifying.
>
> > BTW, in 1.5 gwt supports serialization of objects implementing
> > java.io.Serializable.
> > So just by extendingExceptionit would work.
>
> > Thanks again,
> > Jossey.
>
> > On Nov 26, 8:56 am, Ravi M <mund...@gmail.com> wrote:
>
> > > Jossey
>
> > > Quite the contrary, actually. I started off with a customexception
> > > which was merely a child class ofExceptionthat I intended to use in
> > > RPC calls (i.e. service methods throw it). This actually didn't work
> > > in the ways expected, i.e. when the server threw theexceptionI was
> > > unable to retrieve theexceptionmessage in the client, or find out
> > > what the cause was. Which is when some digging around revealed that if
> > > you want to send theexceptionover the wire, it has to be a GWT
> > > SerializableException.
>
> > > This is with 1.4.6x, of course this may have changed in 1.5.x, I am
> > > not sure.
>
> > > Ravi
>
> > > On Nov 26, 5:58 pm, jossey <joss...@gmail.com> wrote:
>
> > > > Hi,
> > > > Why are we extending SerializableException?
> > > > Is that because the starting example had it so?
> > > > I think it kinds of gives out a wrong message. ...
> > > > just extendsExceptionshould do.
>
> > > > not very relevant to the discussion.... anyways...
>
> > > > Jossey.
>
> > > > On Nov 25, 3:33 pm, Ravi M <mund...@gmail.com> wrote:
>
> > > > > Ah me. Step 1 should read:
>
> > > > > 1. Declare your RPCexceptionlike so:
>
> > > > > public class MyRPCException extends SerializableException {
> > > > >     public MyRPCException() {
> > > > >         super();
> > > > >     }
>
> > > > >     public MyRPCException(String message) {
> > > > >         super(message);
> > > > >     }
>
> > > > >     //... other stuff?
>
> > > > > }
>
> > > > > On Nov 26, 1:32 am, Ravi M <mund...@gmail.com> wrote:
>
> > > > > > Satya,
>
> > > > > > The following should work.
>
> > > > > > 1. Declare your RPCexceptionlike so:
> > > > > > > throwing thatexceptionfrom my Service and check for that in my
> > > > > > > onFailure class.
>
> > > > > > > public void onFailure( Throwable caught )
> > > > > > >                    {
> > > > > > >                               if ( caught.getMessage().contains
> > > > > > > ( "IllegalDateRangeException" ) )
>
> > > > > > >                                     //handle my class.
> > > > > > >                     }
>
> > > > > > > Thnak you,
> > > > > > > Satya
>
> > > > > > > On Nov 25, 1:38 pm, "olivier nouguier" <olivier.nougu...@gmail.com>
> > > > > > > wrote:
>
> > > > > > > > On Tue, Nov 25, 2008 at 8:14 PM, satya <satya.mu...@gmail.com> wrote:
>
> > > > > > > > > Also
>
> > > > > > > > > when i call the actual method, does theexceptionget caught in the
> > > > > > > > > failure method or in my catch method?
>
> > > > > > > > > For example:
>
> > > > > > > > > AsyncCallback callBack = new AsyncCallback() {
> > > > > > > > >                    public void onFailure( Throwable caught )
> > > > > > > > >                    {
> > > > > > > > >                        //Is theexceptioncaught here?????
> > > > > > > > here !
> > > > > > > > >                    }
>
> > > > > > > > >                    public void onSuccess( Object result )
> > > > > > > > >                    {
>
> > > > > > > > >                   }
> > > > > > > > > try
> > > > > > > > >                {
> > > > > > > > >                    createNewRuleService.createRule( screenType,
> > > > > > > > > eventRuleForm, callBack );
> > > > > > > > >                }
> > > > > > > > >                catch ( IllegalDateRangeException e )
> > > > > > > > >                {
> > > > > > > > >                  ////  or theexceptionis caught here?
>
> > > > > > > > >                }
>
> > > > > > > > You should  notice the differences between RemoteInterface and its
> > > > > > > > Asyn counter part:
> > > > > > > > * return type: the asyn interface method doesn't have return value
> > > > > > > > ===>  it will be pass as parameter  asynchronously in onSuccess
> > > > > > > > callback.
> > > > > > > > *exception: are not part of the async method  ===>  it will be pass
> > > > > > > > as parameter  asynchronously in onFailure callback.
>
> > > > > > > > > Thank you very much for your help. I appreciate it.
>
> > > > > > > > > Satya
>
> > > > > > > > > On Nov 25, 1:10 pm, satya <satya.mu...@gmail.com> wrote:
> > > > > > > > >> How do i specify in the service signature that this is GWT
> > > > > > > > >> serializable?
>
> > > > > > > > >> my method signature looks like:
> > > > > > > > >>  public String create(Rule rule) throws IllegalDateRangeException;
>
> > > > > > > > >> On Nov 23, 5:23 am, "olivier nouguier" <olivier.nougu...@gmail.com>
> > > > > > > > >> wrote:
>
> > > > > > > > >> > On Sun, Nov 23, 2008 at 6:58 AM, satya <satya.mu...@gmail.com> wrote:
>
> > > > > > > > >> > > Hi,
>
> > > > > > > > >> > > Can i pass exceptions between client and server.
> > > > > > > > >> > > Can i have an RPC serice with method that throw an user defined
> > > > > > > > >> > >exceptionand handle thatexceptionin the client side?
>
> > > > > > > > >> > Yes as long as your exeption is declared in the Service Signature (to
> > > > > > > > >> > be declared as "GWT serialisable").
>
> > > > > > > > >> > > I created an userdefinedexception:
> > > > > > > > >> > > ------------------------------------------------------
> > > > > > > > >> > > public class IllegalDateRangeException extends SerializationException
> > > > > > > > >> > > implements IsSerializable
> > > > > > > > >> > > {
> > > > > > > > >> > >    private String errorMessage = null;
>
> > > > > > > > >> > >    public IllegalDateRangeException ( String error )
> > > > > > > > >> > >    {
> > > > > > > > >> > >        errorMessage = error;
> > > > > > > > >> > >    }
>
> > > > > > > > >> > >    public String toString()
> > > > > > > > >> > >    {
> > > > > > > > >> > >        return "Exceptionoccurred: " + errorMessage;
> > > > > > > > >> > >    }
>
> > > > > > > > >> > >    public String getErrorMessage()
> > > > > > > > >> > >    {
> > > > > > > > >> > >        return errorMessage;
> > > > > > > > >> > >    }
>
> > > > > > > > >> > >    public void setErrorMessage( String errorMessage )
> > > > > > > > >> > >    {
> > > > > > > > >> > >        this.errorMessage = errorMessage;
> > > > > > > > >> > >    }
>
> > > > > > > > >> > > }
>
> > > > > > > > >> > > and my RPC method throws thisexception:
> > > > > > > > >> > > ------------------------------------------------
> > > > > > > > >> > >    public String create(Rule rule) throws IllegalDateRangeException;
>
> > > > > > > > >> > > and i want to catch this
>
> ...
>
> read more »

Suri

unread,
Dec 24, 2008, 11:43:13 AM12/24/08
to Google Web Toolkit
Correction - This line
public void load(AsyncCallback<ArrayList<SomeType>>)

Is meant to be
public void load(AsyncCallback<ArrayList<SomeType>> callback)

Thanks

On Dec 24, 11:23 am, Suri <sviswanad...@gmail.com> wrote:
> Hey all,
> I was looking for a solution to theexceptionhandlingdescribed above
> and tried the technique suggested. However, when I implement theexceptionin the onFailure method as shown, eclipse barfs and shows a
> "unhandledexceptiontype throwable" error at me. Same thing happens
> when I try to compile anyway.
>
> - I'm using GWT 1.5 and tried extending theexceptionwith bothExceptionas well as SerializationException
> - Also, I'm implementing a fascade pattern as described in the GWT in
> Action Book for the RPC calls.
>
> Here's a scenario of what I'm trying
>
> public class MyRPCException extendsException
> {
>    // same definition ofexceptionas above really.
> ...
>
> read more »

olivier nouguier

unread,
Dec 25, 2008, 3:50:51 AM12/25/08
to Google-We...@googlegroups.com
public void onFailure(Throwable caught) {
Window.alert("DING");
}

should be enougth !!!
eventually:

public void onFailure(Throwable caught) {
YourTypedException e = (YourTypedException)caught;
Window.alert(e.getCustomTypedExceptionCode());
}

Reply all
Reply to author
Forward
0 new messages