terminate a handler execution in JavaScript
I need to terminate a handler execution in JavaScript in order to allow methods/handlers to be executed in a Web page. For example, the following piece of code illustrates what I need:
function handler() {
//doing many things 1
internalProcess()
//doing many things 2 (it is not executed)
}
function internalProcess() {
//doing many things 3
//terminate the handler execution
}
I have seen some solutions using throw
but they are not working for me because sometimes other functions in the handler captures this exception and the handler continues its execution.
javascript handler
add a comment |
I need to terminate a handler execution in JavaScript in order to allow methods/handlers to be executed in a Web page. For example, the following piece of code illustrates what I need:
function handler() {
//doing many things 1
internalProcess()
//doing many things 2 (it is not executed)
}
function internalProcess() {
//doing many things 3
//terminate the handler execution
}
I have seen some solutions using throw
but they are not working for me because sometimes other functions in the handler captures this exception and the handler continues its execution.
javascript handler
2
throw
, is the correct way.sometimes other functions in the handler captures this exception
then maybe they should not. It's hard to tell without a more concrete example.
– Keith
Nov 19 '18 at 17:17
I am not sure what you are asking.... If they catch the errors, not sure what you can do....
– epascarello
Nov 19 '18 at 17:17
Could just add areturn;
afterinternalProcess()
in the first handler?
– apokryfos
Nov 19 '18 at 17:18
1
There is no mechanism that directly does what you're asking;throw
is about as close as you can get.
– Pointy
Nov 19 '18 at 17:20
add a comment |
I need to terminate a handler execution in JavaScript in order to allow methods/handlers to be executed in a Web page. For example, the following piece of code illustrates what I need:
function handler() {
//doing many things 1
internalProcess()
//doing many things 2 (it is not executed)
}
function internalProcess() {
//doing many things 3
//terminate the handler execution
}
I have seen some solutions using throw
but they are not working for me because sometimes other functions in the handler captures this exception and the handler continues its execution.
javascript handler
I need to terminate a handler execution in JavaScript in order to allow methods/handlers to be executed in a Web page. For example, the following piece of code illustrates what I need:
function handler() {
//doing many things 1
internalProcess()
//doing many things 2 (it is not executed)
}
function internalProcess() {
//doing many things 3
//terminate the handler execution
}
I have seen some solutions using throw
but they are not working for me because sometimes other functions in the handler captures this exception and the handler continues its execution.
javascript handler
javascript handler
edited Nov 19 '18 at 17:15
j08691
166k20192213
166k20192213
asked Nov 19 '18 at 17:14
Naive DeveloperNaive Developer
13829
13829
2
throw
, is the correct way.sometimes other functions in the handler captures this exception
then maybe they should not. It's hard to tell without a more concrete example.
– Keith
Nov 19 '18 at 17:17
I am not sure what you are asking.... If they catch the errors, not sure what you can do....
– epascarello
Nov 19 '18 at 17:17
Could just add areturn;
afterinternalProcess()
in the first handler?
– apokryfos
Nov 19 '18 at 17:18
1
There is no mechanism that directly does what you're asking;throw
is about as close as you can get.
– Pointy
Nov 19 '18 at 17:20
add a comment |
2
throw
, is the correct way.sometimes other functions in the handler captures this exception
then maybe they should not. It's hard to tell without a more concrete example.
– Keith
Nov 19 '18 at 17:17
I am not sure what you are asking.... If they catch the errors, not sure what you can do....
– epascarello
Nov 19 '18 at 17:17
Could just add areturn;
afterinternalProcess()
in the first handler?
– apokryfos
Nov 19 '18 at 17:18
1
There is no mechanism that directly does what you're asking;throw
is about as close as you can get.
– Pointy
Nov 19 '18 at 17:20
2
2
throw
, is the correct way. sometimes other functions in the handler captures this exception
then maybe they should not. It's hard to tell without a more concrete example.– Keith
Nov 19 '18 at 17:17
throw
, is the correct way. sometimes other functions in the handler captures this exception
then maybe they should not. It's hard to tell without a more concrete example.– Keith
Nov 19 '18 at 17:17
I am not sure what you are asking.... If they catch the errors, not sure what you can do....
– epascarello
Nov 19 '18 at 17:17
I am not sure what you are asking.... If they catch the errors, not sure what you can do....
– epascarello
Nov 19 '18 at 17:17
Could just add a
return;
after internalProcess()
in the first handler?– apokryfos
Nov 19 '18 at 17:18
Could just add a
return;
after internalProcess()
in the first handler?– apokryfos
Nov 19 '18 at 17:18
1
1
There is no mechanism that directly does what you're asking;
throw
is about as close as you can get.– Pointy
Nov 19 '18 at 17:20
There is no mechanism that directly does what you're asking;
throw
is about as close as you can get.– Pointy
Nov 19 '18 at 17:20
add a comment |
1 Answer
1
active
oldest
votes
throw
would be appropriate if you are handling errors in your internalProcess
. You would simply need to try / catch
in you handler:
function handler() {
try {
console.log('doing many things 1');
internalProcess();
console.log('doing many things 2');
} catch (e) {
console.log('interrupted:', e.message);
}
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
throw new Error('a message');
}
handler();
If you just want to sometimes stop execution of handler()
depending on computations that happen in internalProcess()
, here's a way without using throw:
Simply make the return value of internalProcess()
tell handler()
whether to continue running or not:
function handler() {
console.log('doing many things 1');
const shouldContinue = internalProcess();
if (!shouldContinue) {
return;
}
console.log('doing many things 2');
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
return false;
}
handler();
} catch (e) {
you really need to be sure this is what you want,. eg. the function handler will now always succeed, as you don't re-throw. Generally speaking exception handling is something that's handled right at the root of your application,.
– Keith
Nov 19 '18 at 17:34
Thanks for the feedback! I think it really depends on your needs, I'm not sure what the use case is here, which is why I recommend the second option. There are definitely use cases where you want to handle known exceptions, for example on an event handler since there is no other function directly calling it. In this example, I assumedhandler
was an event handler, and wanted to do error handling within it, but yes, if you want to expose errors to users you would need to rethrow.
– lucascaro
Nov 19 '18 at 17:39
The second solution doesn't work for me, because I have to interrupt an execution. My only problem with the first one if the call ofinternalProcess
(which triggers the exception) is inside of another function,foo
, that captures this exception becausefoo
usescatch(e)
. Maybe If creates a particular object exception, it is not captured.
– Naive Developer
Nov 19 '18 at 18:52
I see... you should try posting a Minimal, Complete, and Verifiable example, that would help us understand the specifics of your problem and provide better answers.
– lucascaro
Nov 20 '18 at 2:59
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%2f53379632%2fterminate-a-handler-execution-in-javascript%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
throw
would be appropriate if you are handling errors in your internalProcess
. You would simply need to try / catch
in you handler:
function handler() {
try {
console.log('doing many things 1');
internalProcess();
console.log('doing many things 2');
} catch (e) {
console.log('interrupted:', e.message);
}
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
throw new Error('a message');
}
handler();
If you just want to sometimes stop execution of handler()
depending on computations that happen in internalProcess()
, here's a way without using throw:
Simply make the return value of internalProcess()
tell handler()
whether to continue running or not:
function handler() {
console.log('doing many things 1');
const shouldContinue = internalProcess();
if (!shouldContinue) {
return;
}
console.log('doing many things 2');
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
return false;
}
handler();
} catch (e) {
you really need to be sure this is what you want,. eg. the function handler will now always succeed, as you don't re-throw. Generally speaking exception handling is something that's handled right at the root of your application,.
– Keith
Nov 19 '18 at 17:34
Thanks for the feedback! I think it really depends on your needs, I'm not sure what the use case is here, which is why I recommend the second option. There are definitely use cases where you want to handle known exceptions, for example on an event handler since there is no other function directly calling it. In this example, I assumedhandler
was an event handler, and wanted to do error handling within it, but yes, if you want to expose errors to users you would need to rethrow.
– lucascaro
Nov 19 '18 at 17:39
The second solution doesn't work for me, because I have to interrupt an execution. My only problem with the first one if the call ofinternalProcess
(which triggers the exception) is inside of another function,foo
, that captures this exception becausefoo
usescatch(e)
. Maybe If creates a particular object exception, it is not captured.
– Naive Developer
Nov 19 '18 at 18:52
I see... you should try posting a Minimal, Complete, and Verifiable example, that would help us understand the specifics of your problem and provide better answers.
– lucascaro
Nov 20 '18 at 2:59
add a comment |
throw
would be appropriate if you are handling errors in your internalProcess
. You would simply need to try / catch
in you handler:
function handler() {
try {
console.log('doing many things 1');
internalProcess();
console.log('doing many things 2');
} catch (e) {
console.log('interrupted:', e.message);
}
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
throw new Error('a message');
}
handler();
If you just want to sometimes stop execution of handler()
depending on computations that happen in internalProcess()
, here's a way without using throw:
Simply make the return value of internalProcess()
tell handler()
whether to continue running or not:
function handler() {
console.log('doing many things 1');
const shouldContinue = internalProcess();
if (!shouldContinue) {
return;
}
console.log('doing many things 2');
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
return false;
}
handler();
} catch (e) {
you really need to be sure this is what you want,. eg. the function handler will now always succeed, as you don't re-throw. Generally speaking exception handling is something that's handled right at the root of your application,.
– Keith
Nov 19 '18 at 17:34
Thanks for the feedback! I think it really depends on your needs, I'm not sure what the use case is here, which is why I recommend the second option. There are definitely use cases where you want to handle known exceptions, for example on an event handler since there is no other function directly calling it. In this example, I assumedhandler
was an event handler, and wanted to do error handling within it, but yes, if you want to expose errors to users you would need to rethrow.
– lucascaro
Nov 19 '18 at 17:39
The second solution doesn't work for me, because I have to interrupt an execution. My only problem with the first one if the call ofinternalProcess
(which triggers the exception) is inside of another function,foo
, that captures this exception becausefoo
usescatch(e)
. Maybe If creates a particular object exception, it is not captured.
– Naive Developer
Nov 19 '18 at 18:52
I see... you should try posting a Minimal, Complete, and Verifiable example, that would help us understand the specifics of your problem and provide better answers.
– lucascaro
Nov 20 '18 at 2:59
add a comment |
throw
would be appropriate if you are handling errors in your internalProcess
. You would simply need to try / catch
in you handler:
function handler() {
try {
console.log('doing many things 1');
internalProcess();
console.log('doing many things 2');
} catch (e) {
console.log('interrupted:', e.message);
}
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
throw new Error('a message');
}
handler();
If you just want to sometimes stop execution of handler()
depending on computations that happen in internalProcess()
, here's a way without using throw:
Simply make the return value of internalProcess()
tell handler()
whether to continue running or not:
function handler() {
console.log('doing many things 1');
const shouldContinue = internalProcess();
if (!shouldContinue) {
return;
}
console.log('doing many things 2');
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
return false;
}
handler();
throw
would be appropriate if you are handling errors in your internalProcess
. You would simply need to try / catch
in you handler:
function handler() {
try {
console.log('doing many things 1');
internalProcess();
console.log('doing many things 2');
} catch (e) {
console.log('interrupted:', e.message);
}
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
throw new Error('a message');
}
handler();
If you just want to sometimes stop execution of handler()
depending on computations that happen in internalProcess()
, here's a way without using throw:
Simply make the return value of internalProcess()
tell handler()
whether to continue running or not:
function handler() {
console.log('doing many things 1');
const shouldContinue = internalProcess();
if (!shouldContinue) {
return;
}
console.log('doing many things 2');
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
return false;
}
handler();
function handler() {
try {
console.log('doing many things 1');
internalProcess();
console.log('doing many things 2');
} catch (e) {
console.log('interrupted:', e.message);
}
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
throw new Error('a message');
}
handler();
function handler() {
try {
console.log('doing many things 1');
internalProcess();
console.log('doing many things 2');
} catch (e) {
console.log('interrupted:', e.message);
}
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
throw new Error('a message');
}
handler();
function handler() {
console.log('doing many things 1');
const shouldContinue = internalProcess();
if (!shouldContinue) {
return;
}
console.log('doing many things 2');
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
return false;
}
handler();
function handler() {
console.log('doing many things 1');
const shouldContinue = internalProcess();
if (!shouldContinue) {
return;
}
console.log('doing many things 2');
}
function internalProcess() {
console.log('doing many things 3');
//terminate the handler execution
return false;
}
handler();
answered Nov 19 '18 at 17:25
lucascarolucascaro
3,72611731
3,72611731
} catch (e) {
you really need to be sure this is what you want,. eg. the function handler will now always succeed, as you don't re-throw. Generally speaking exception handling is something that's handled right at the root of your application,.
– Keith
Nov 19 '18 at 17:34
Thanks for the feedback! I think it really depends on your needs, I'm not sure what the use case is here, which is why I recommend the second option. There are definitely use cases where you want to handle known exceptions, for example on an event handler since there is no other function directly calling it. In this example, I assumedhandler
was an event handler, and wanted to do error handling within it, but yes, if you want to expose errors to users you would need to rethrow.
– lucascaro
Nov 19 '18 at 17:39
The second solution doesn't work for me, because I have to interrupt an execution. My only problem with the first one if the call ofinternalProcess
(which triggers the exception) is inside of another function,foo
, that captures this exception becausefoo
usescatch(e)
. Maybe If creates a particular object exception, it is not captured.
– Naive Developer
Nov 19 '18 at 18:52
I see... you should try posting a Minimal, Complete, and Verifiable example, that would help us understand the specifics of your problem and provide better answers.
– lucascaro
Nov 20 '18 at 2:59
add a comment |
} catch (e) {
you really need to be sure this is what you want,. eg. the function handler will now always succeed, as you don't re-throw. Generally speaking exception handling is something that's handled right at the root of your application,.
– Keith
Nov 19 '18 at 17:34
Thanks for the feedback! I think it really depends on your needs, I'm not sure what the use case is here, which is why I recommend the second option. There are definitely use cases where you want to handle known exceptions, for example on an event handler since there is no other function directly calling it. In this example, I assumedhandler
was an event handler, and wanted to do error handling within it, but yes, if you want to expose errors to users you would need to rethrow.
– lucascaro
Nov 19 '18 at 17:39
The second solution doesn't work for me, because I have to interrupt an execution. My only problem with the first one if the call ofinternalProcess
(which triggers the exception) is inside of another function,foo
, that captures this exception becausefoo
usescatch(e)
. Maybe If creates a particular object exception, it is not captured.
– Naive Developer
Nov 19 '18 at 18:52
I see... you should try posting a Minimal, Complete, and Verifiable example, that would help us understand the specifics of your problem and provide better answers.
– lucascaro
Nov 20 '18 at 2:59
} catch (e) {
you really need to be sure this is what you want,. eg. the function handler will now always succeed, as you don't re-throw. Generally speaking exception handling is something that's handled right at the root of your application,.– Keith
Nov 19 '18 at 17:34
} catch (e) {
you really need to be sure this is what you want,. eg. the function handler will now always succeed, as you don't re-throw. Generally speaking exception handling is something that's handled right at the root of your application,.– Keith
Nov 19 '18 at 17:34
Thanks for the feedback! I think it really depends on your needs, I'm not sure what the use case is here, which is why I recommend the second option. There are definitely use cases where you want to handle known exceptions, for example on an event handler since there is no other function directly calling it. In this example, I assumed
handler
was an event handler, and wanted to do error handling within it, but yes, if you want to expose errors to users you would need to rethrow.– lucascaro
Nov 19 '18 at 17:39
Thanks for the feedback! I think it really depends on your needs, I'm not sure what the use case is here, which is why I recommend the second option. There are definitely use cases where you want to handle known exceptions, for example on an event handler since there is no other function directly calling it. In this example, I assumed
handler
was an event handler, and wanted to do error handling within it, but yes, if you want to expose errors to users you would need to rethrow.– lucascaro
Nov 19 '18 at 17:39
The second solution doesn't work for me, because I have to interrupt an execution. My only problem with the first one if the call of
internalProcess
(which triggers the exception) is inside of another function, foo
, that captures this exception because foo
uses catch(e)
. Maybe If creates a particular object exception, it is not captured.– Naive Developer
Nov 19 '18 at 18:52
The second solution doesn't work for me, because I have to interrupt an execution. My only problem with the first one if the call of
internalProcess
(which triggers the exception) is inside of another function, foo
, that captures this exception because foo
uses catch(e)
. Maybe If creates a particular object exception, it is not captured.– Naive Developer
Nov 19 '18 at 18:52
I see... you should try posting a Minimal, Complete, and Verifiable example, that would help us understand the specifics of your problem and provide better answers.
– lucascaro
Nov 20 '18 at 2:59
I see... you should try posting a Minimal, Complete, and Verifiable example, that would help us understand the specifics of your problem and provide better answers.
– lucascaro
Nov 20 '18 at 2:59
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%2f53379632%2fterminate-a-handler-execution-in-javascript%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
2
throw
, is the correct way.sometimes other functions in the handler captures this exception
then maybe they should not. It's hard to tell without a more concrete example.– Keith
Nov 19 '18 at 17:17
I am not sure what you are asking.... If they catch the errors, not sure what you can do....
– epascarello
Nov 19 '18 at 17:17
Could just add a
return;
afterinternalProcess()
in the first handler?– apokryfos
Nov 19 '18 at 17:18
1
There is no mechanism that directly does what you're asking;
throw
is about as close as you can get.– Pointy
Nov 19 '18 at 17:20