throwing a Serializable RuntimeException in OnNext requires OnNext value to be Serializable as well
when throwing a runtime exception during OnNext, the exception is wrapped within OnErrorThrowable$OnNextValue, which features access to the value emitted in OnNext. this feature comes at a cost - If one would wish to serialize/deserialize the exception or any object containing that exception, one would also be required to have the type of item emitted by OnNext also implement Serializable (which could lead to whole hierarchy of objects be serializable). While the original runtime exception thrown is serializable, the OnErrorThrowable$OnNextValue which includes reference to the onnext emitted item, isn't. This leads to java.io.NotSerializableException thrown when trying to serialize the exception:
Caused by: java.io.NotSerializableException: com.example.SomeUnserializableObject
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.lang.Throwable.writeObject(Throwable.java:436)
at java.lang.reflect.Method.invoke(Native Method)
at java.io.ObjectO
I dont see any way around it other than avoiding throwing of RuntimeException during onNext, and instead move to flatmap and return Observable.error(exception).
We can mark the value as transient in the exception.
On Thu, Sep 15, 2016 at 3:29 PM saharh [email protected] wrote:
when throwing a runtime exception during OnNext, the exception is wrapped within OnErrorThrowable$OnNextValue, which features access to the value emitted in OnNext. this feature comes at a cost - If one would wish to serialize/deserialize the exception or any object containing that exception, one would also be required to have the type of item emitted by OnNext also implement Serializable (which could lead to whole hierarchy of objects be serializable). While the original runtime exception thrown is serializable, the OnErrorThrowable$OnNextValue which includes reference to the onnext emitted item, isn't. This leads to java.io.NotSerializableException thrown when trying to serialize the exception:
Caused by: java.io.NotSerializableException: com.example.sample.SomeUnserializableObject at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344) at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461) at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959) at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360) at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054) at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384) at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461) at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959) at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360) at java.lang.Throwable.writeObject(Throwable.java:436) at java.lang.reflect.Method.invoke(Native Method) at java.io.ObjectOI dont see any way around it other than avoiding throwing of RuntimeException during onNext, and instead move to flatmap and return Observable.error(exception).
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ReactiveX/RxJava/issues/4562, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEd3gv1EWHy9yKNEmMMtIbHjEAqpjks5qqZyhgaJpZM4J-QSY .
I must ask: what's your use case where you want to serialize exceptions? Are passing them over to some other process maybe?
It is a rather rare use case, but i do pass a result which might include an exception between activities in android, which requires the object to be serializable
See #4841
Closing via #4841