Error typing 2.x
I'm developing a library for picking images in Android with RxJava 2.x. I've defined some exceptions such as PermissionException or FileNotFoundException. Because of the implementation of RxJava, when calling the library methods and implementing a observer, these exceptions are received as a Throwable. I think this implementation ruins all the power of the try-catch blocks of Java. For example:
Without RxJava:
library definition:
public File pickImage() throws PermissionDeniedException, FileNotFoundException;
calling library:
try {
pickImage();
} catch (PermissionDeniedException e) {
// doSomething
} catch (FileNotFoundException e2) {
// doSomethingDiferent
}
With RxJava:
library definition:
public Observable<File> pickImage();
calling library:
pickImage().subscribe(new DisposableObserver<File>() {
public void onNext(File f)...
public void onComplete()...
public void onError(Throwable t) {
// doSomething (casting t to concrete exceptions and I don't know what exceptions are thrown)
}
});
So my question is:
why can't be the onError method of the observer typed like onNext method?
I believe that the Observer could be typed with several types (one for each exception), like it's done with Functions3-9.
Regards.
Hi.
ruins all the power of the try-catch blocks of Java
Try-catch blocks don't work in an asynchronous or deferred-synchronous environment. You have to signal an error, not throw it and you can't catch it but receive it through a method call.
why can't be the onError method of the observer typed like onNext method
First, there is no need for extra emphasis on your question.
Second, it doesn't work. There are two reasons for this:
- There are no union types in Java.
You can't define a type which combines RuntimeException with IOException and even if you could, you'd have to pick the resulting union type apart with if (exception instanceof RuntimException) or pattern matching which isn't available in Java yet.
- Exception types don't compose well, particularly, they compose in a contravariant manner.
If you merge a stream of RuntimeException with a stream of IOException, what could be the combined exception type? A type E where RuntimeException extends E and IOException extends E. Specifically, E could be Exception or its supertype Throwable. Since we can't go further up, Throwable is the most generic type that allows merging of any exception types.
On a high level view, you'd think the compiler can do this inference but Java 6's type inference is not capable of deducing the supertype (or many contravariant bound) unfortunately.
If there were union types, the merged error type could be RuntimeException | IOException but pretty quickly you'd end up with dozens of unioned exception type and the signature would look ugly. Plus without advanced type inference and language support, you'd have to manually specify the lengthy generics signatures. Even if this was solved, you'd still have to pick the resulting dozen unioned error apart with ifs/pattern matching.
Hi,
Thanks for the response. First, sorry for extra emphasis, it's my first question in Github.
Second, I think I haven't explained it well. Would the following solution be possible?
public Observable<File,PermissionDeniedException,FileNotFoundException > pickImage();
pickImage().subscribe(new DisposableObserver<File,PermissionDeniedException,FileNotFoundException>() {
public void onNext(File f)...
public void onComplete()...
public void onError(PermissionDeniedException e) {
// doSomething
}
public void onError(FileNotFoundException e) {
// doSomethingDiferent
}
});
I wouldn't want one onError method, I'd want one error method for each exception.
No. Generics won't allow such overloads due to erasure and even if, there is simply no static way to match up those onErrors. Plus, you'd have to have all operators rewritten for 2-onError types, then 3-onError types and so on.
You can turn errors into values of the stream and use power of type system to tell clients of your library about possible outcomes of particular operations.
(In Kotlin that is what sealed classes are for. In Java that's a little bit less easy to do but still possible with some kind of "Either" implementation).
On Mon, Mar 27, 2017, 21:06 Pablo Alonso González [email protected] wrote:
Hi,
Thanks for the response. First, sorry for extra emphasis, it's my first question in Github.
public Observable<File,PermissionDeniedException,FileNotFoundException > pickImage();
pickImage().subscribe(new DisposableObserver<File,PermissionDeniedException,FileNotFoundException>() { public void onNext(File f)... public void onComplete()... public void onError(PermissionDeniedException e) { // doSomething } public void onError(FileNotFoundException e) { // doSomethingDiferent } });
— 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/5221#issuecomment-289422484, or mute the thread https://github.com/notifications/unsubscribe-auth/AA7B3PeM3n5q32MUeQip06DLMFyNCiDsks5rp5hvgaJpZM4MoRHf .
Looks like this question has been answered. If you have further input on the issue, don't hesitate to reopen this issue or post a new one.