|Serializing exceptions||Jérôme||9/13/10 6:44 AM|
I just started using GSon for a project I am woking on.
I have a strange problem, for which I found nothing on the web :
Whenever I try to serialize an exception, GSon raises an
IllegalStateException with message :
circular reference error
Offending field: cause
Here is the code I have to reproduce this :
Gson serializer = new GsonBuilder().create();
Exception theException = new Exception();
String result = serializer.toJson(theException);
In such a case, theException.getCause() returns null, but the debugger
tells me that the cause field points to the exception itself,
therefore causing a circular reference.
I am a bit lost. Is this normal, and if so, how can I handle
Exception's serialization ?
Thanks for your help !
|Re: [google-gson:763]: Serializing exceptions||Pawel||9/13/10 11:21 AM|
Well, from the source code of java.lang.Throwable (file version 1.56,
from some SDK 1.6):
So, you have three (exclusive) choices:
1) If you are serializing your own exceptions, modify all of your
Also, attempting to send serialized exception code over the wire may
|Re: : Serializing exceptions||Jérôme||9/14/10 6:37 AM|
Oups, I did not thought looking at Throwable's sources.
My idea was to serialize the exception in case of service failure, to
embed the message giving details on why the service failed in the
The workaround I used is to extract the message, and send it instead
of the exception.
Thanks for your help. Next time, i will dig a bit more before