In RxJava/RxKotlin, what are the differences between returning a Completable.error(Exception()) and throwing?
What are the differences in the following cases:
fun a(params: String) = Completable.fromAction {
if (params.isEmpty()) {
throw EmptyRequiredFieldException()
}
}
VS
fun b(params: String) = if(params.isEmpty())
Completable.error(EmptyRequiredFieldException())
else
Completable.complete()
Specifically in the context of android, if it matters (even though I don't think it does)
Thanks!
error-handling kotlin observable rx-java2 rx-kotlin
add a comment |
What are the differences in the following cases:
fun a(params: String) = Completable.fromAction {
if (params.isEmpty()) {
throw EmptyRequiredFieldException()
}
}
VS
fun b(params: String) = if(params.isEmpty())
Completable.error(EmptyRequiredFieldException())
else
Completable.complete()
Specifically in the context of android, if it matters (even though I don't think it does)
Thanks!
error-handling kotlin observable rx-java2 rx-kotlin
add a comment |
What are the differences in the following cases:
fun a(params: String) = Completable.fromAction {
if (params.isEmpty()) {
throw EmptyRequiredFieldException()
}
}
VS
fun b(params: String) = if(params.isEmpty())
Completable.error(EmptyRequiredFieldException())
else
Completable.complete()
Specifically in the context of android, if it matters (even though I don't think it does)
Thanks!
error-handling kotlin observable rx-java2 rx-kotlin
What are the differences in the following cases:
fun a(params: String) = Completable.fromAction {
if (params.isEmpty()) {
throw EmptyRequiredFieldException()
}
}
VS
fun b(params: String) = if(params.isEmpty())
Completable.error(EmptyRequiredFieldException())
else
Completable.complete()
Specifically in the context of android, if it matters (even though I don't think it does)
Thanks!
error-handling kotlin observable rx-java2 rx-kotlin
error-handling kotlin observable rx-java2 rx-kotlin
asked Nov 14 '18 at 18:59
Yuri MachioniYuri Machioni
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
According to documentation,
If the Action throws an exception, the respective Throwable is delivered to the downstream via CompletableObserver.onError(Throwable), except when the downstream has disposed this Completable source. In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.
So both of two ways you described are similar (except when the downstream has disposed). Note, that first approach (with manually throwing exception) allow to modify behavior of Completable
at runtime. And second one - statically defined as you return particular type of Completable
and can't modify it.
What to choose depends on your needs.
So that means that in the first case the app will crash if the subscriber is disposed (if i'm using the default global error handler) and in the second it won't ?
– Yuri Machioni
Nov 15 '18 at 12:11
I suppose, yes.
– ConstOrVar
Nov 15 '18 at 13:07
RxJava 2.1.1 introduced an experimental tryOnError(t: Throwable) method on SingleEmitter. It's documentation says: "Attempts to emit the specified {@code Throwable} error if the downstream * hasn't cancelled the sequence or is otherwise terminated, returning false * if the emission is not allowed to happen due to lifecycle restrictions. * <p> * Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called * if the error could not be delivered." So I don't know how this fits what you said.
– Yuri Machioni
Nov 19 '18 at 15:46
Seems to me that both behave the same except tryOnError(), but if thats the case, everything before it would crash if disposal happens before onError ? How could I safely ignore a network error if the user (for an example) navigates away before the error is returned then ? This is really confusing.
– Yuri Machioni
Nov 19 '18 at 15:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307056%2fin-rxjava-rxkotlin-what-are-the-differences-between-returning-a-completable-err%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to documentation,
If the Action throws an exception, the respective Throwable is delivered to the downstream via CompletableObserver.onError(Throwable), except when the downstream has disposed this Completable source. In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.
So both of two ways you described are similar (except when the downstream has disposed). Note, that first approach (with manually throwing exception) allow to modify behavior of Completable
at runtime. And second one - statically defined as you return particular type of Completable
and can't modify it.
What to choose depends on your needs.
So that means that in the first case the app will crash if the subscriber is disposed (if i'm using the default global error handler) and in the second it won't ?
– Yuri Machioni
Nov 15 '18 at 12:11
I suppose, yes.
– ConstOrVar
Nov 15 '18 at 13:07
RxJava 2.1.1 introduced an experimental tryOnError(t: Throwable) method on SingleEmitter. It's documentation says: "Attempts to emit the specified {@code Throwable} error if the downstream * hasn't cancelled the sequence or is otherwise terminated, returning false * if the emission is not allowed to happen due to lifecycle restrictions. * <p> * Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called * if the error could not be delivered." So I don't know how this fits what you said.
– Yuri Machioni
Nov 19 '18 at 15:46
Seems to me that both behave the same except tryOnError(), but if thats the case, everything before it would crash if disposal happens before onError ? How could I safely ignore a network error if the user (for an example) navigates away before the error is returned then ? This is really confusing.
– Yuri Machioni
Nov 19 '18 at 15:51
add a comment |
According to documentation,
If the Action throws an exception, the respective Throwable is delivered to the downstream via CompletableObserver.onError(Throwable), except when the downstream has disposed this Completable source. In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.
So both of two ways you described are similar (except when the downstream has disposed). Note, that first approach (with manually throwing exception) allow to modify behavior of Completable
at runtime. And second one - statically defined as you return particular type of Completable
and can't modify it.
What to choose depends on your needs.
So that means that in the first case the app will crash if the subscriber is disposed (if i'm using the default global error handler) and in the second it won't ?
– Yuri Machioni
Nov 15 '18 at 12:11
I suppose, yes.
– ConstOrVar
Nov 15 '18 at 13:07
RxJava 2.1.1 introduced an experimental tryOnError(t: Throwable) method on SingleEmitter. It's documentation says: "Attempts to emit the specified {@code Throwable} error if the downstream * hasn't cancelled the sequence or is otherwise terminated, returning false * if the emission is not allowed to happen due to lifecycle restrictions. * <p> * Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called * if the error could not be delivered." So I don't know how this fits what you said.
– Yuri Machioni
Nov 19 '18 at 15:46
Seems to me that both behave the same except tryOnError(), but if thats the case, everything before it would crash if disposal happens before onError ? How could I safely ignore a network error if the user (for an example) navigates away before the error is returned then ? This is really confusing.
– Yuri Machioni
Nov 19 '18 at 15:51
add a comment |
According to documentation,
If the Action throws an exception, the respective Throwable is delivered to the downstream via CompletableObserver.onError(Throwable), except when the downstream has disposed this Completable source. In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.
So both of two ways you described are similar (except when the downstream has disposed). Note, that first approach (with manually throwing exception) allow to modify behavior of Completable
at runtime. And second one - statically defined as you return particular type of Completable
and can't modify it.
What to choose depends on your needs.
According to documentation,
If the Action throws an exception, the respective Throwable is delivered to the downstream via CompletableObserver.onError(Throwable), except when the downstream has disposed this Completable source. In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.
So both of two ways you described are similar (except when the downstream has disposed). Note, that first approach (with manually throwing exception) allow to modify behavior of Completable
at runtime. And second one - statically defined as you return particular type of Completable
and can't modify it.
What to choose depends on your needs.
answered Nov 14 '18 at 19:43
ConstOrVarConstOrVar
1,094149
1,094149
So that means that in the first case the app will crash if the subscriber is disposed (if i'm using the default global error handler) and in the second it won't ?
– Yuri Machioni
Nov 15 '18 at 12:11
I suppose, yes.
– ConstOrVar
Nov 15 '18 at 13:07
RxJava 2.1.1 introduced an experimental tryOnError(t: Throwable) method on SingleEmitter. It's documentation says: "Attempts to emit the specified {@code Throwable} error if the downstream * hasn't cancelled the sequence or is otherwise terminated, returning false * if the emission is not allowed to happen due to lifecycle restrictions. * <p> * Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called * if the error could not be delivered." So I don't know how this fits what you said.
– Yuri Machioni
Nov 19 '18 at 15:46
Seems to me that both behave the same except tryOnError(), but if thats the case, everything before it would crash if disposal happens before onError ? How could I safely ignore a network error if the user (for an example) navigates away before the error is returned then ? This is really confusing.
– Yuri Machioni
Nov 19 '18 at 15:51
add a comment |
So that means that in the first case the app will crash if the subscriber is disposed (if i'm using the default global error handler) and in the second it won't ?
– Yuri Machioni
Nov 15 '18 at 12:11
I suppose, yes.
– ConstOrVar
Nov 15 '18 at 13:07
RxJava 2.1.1 introduced an experimental tryOnError(t: Throwable) method on SingleEmitter. It's documentation says: "Attempts to emit the specified {@code Throwable} error if the downstream * hasn't cancelled the sequence or is otherwise terminated, returning false * if the emission is not allowed to happen due to lifecycle restrictions. * <p> * Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called * if the error could not be delivered." So I don't know how this fits what you said.
– Yuri Machioni
Nov 19 '18 at 15:46
Seems to me that both behave the same except tryOnError(), but if thats the case, everything before it would crash if disposal happens before onError ? How could I safely ignore a network error if the user (for an example) navigates away before the error is returned then ? This is really confusing.
– Yuri Machioni
Nov 19 '18 at 15:51
So that means that in the first case the app will crash if the subscriber is disposed (if i'm using the default global error handler) and in the second it won't ?
– Yuri Machioni
Nov 15 '18 at 12:11
So that means that in the first case the app will crash if the subscriber is disposed (if i'm using the default global error handler) and in the second it won't ?
– Yuri Machioni
Nov 15 '18 at 12:11
I suppose, yes.
– ConstOrVar
Nov 15 '18 at 13:07
I suppose, yes.
– ConstOrVar
Nov 15 '18 at 13:07
RxJava 2.1.1 introduced an experimental tryOnError(t: Throwable) method on SingleEmitter. It's documentation says: "Attempts to emit the specified {@code Throwable} error if the downstream * hasn't cancelled the sequence or is otherwise terminated, returning false * if the emission is not allowed to happen due to lifecycle restrictions. * <p> * Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called * if the error could not be delivered." So I don't know how this fits what you said.
– Yuri Machioni
Nov 19 '18 at 15:46
RxJava 2.1.1 introduced an experimental tryOnError(t: Throwable) method on SingleEmitter. It's documentation says: "Attempts to emit the specified {@code Throwable} error if the downstream * hasn't cancelled the sequence or is otherwise terminated, returning false * if the emission is not allowed to happen due to lifecycle restrictions. * <p> * Unlike {@link #onError(Throwable)}, the {@code RxJavaPlugins.onError} is not called * if the error could not be delivered." So I don't know how this fits what you said.
– Yuri Machioni
Nov 19 '18 at 15:46
Seems to me that both behave the same except tryOnError(), but if thats the case, everything before it would crash if disposal happens before onError ? How could I safely ignore a network error if the user (for an example) navigates away before the error is returned then ? This is really confusing.
– Yuri Machioni
Nov 19 '18 at 15:51
Seems to me that both behave the same except tryOnError(), but if thats the case, everything before it would crash if disposal happens before onError ? How could I safely ignore a network error if the user (for an example) navigates away before the error is returned then ? This is really confusing.
– Yuri Machioni
Nov 19 '18 at 15:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307056%2fin-rxjava-rxkotlin-what-are-the-differences-between-returning-a-completable-err%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown