Multiple catch in javascript
Is it possible to use multiple catch in JS(ES5 or ES6)
like I describe below (it is only example):
try {
// just an error
throw 1;
}
catch(e if e instanceof ReferenceError) {
// here i would like to manage errors which is 'undefined' type
}
catch(e if typeof e === "string") {
// here i can manage all string exeptions
}
// and so on and so on
catch(e) {
// and finally here i can manage another exeptions
}
finally {
// and a simple finally block
}
This is the same as we have in C#
or in a Java
.
Thanks in advance!
javascript
add a comment |
Is it possible to use multiple catch in JS(ES5 or ES6)
like I describe below (it is only example):
try {
// just an error
throw 1;
}
catch(e if e instanceof ReferenceError) {
// here i would like to manage errors which is 'undefined' type
}
catch(e if typeof e === "string") {
// here i can manage all string exeptions
}
// and so on and so on
catch(e) {
// and finally here i can manage another exeptions
}
finally {
// and a simple finally block
}
This is the same as we have in C#
or in a Java
.
Thanks in advance!
javascript
1
One catch block and then check the type of the exception?
– Icepickle
Nov 18 '15 at 13:53
@Icepickle yeap you are right
– user5548116
Nov 18 '15 at 13:54
add a comment |
Is it possible to use multiple catch in JS(ES5 or ES6)
like I describe below (it is only example):
try {
// just an error
throw 1;
}
catch(e if e instanceof ReferenceError) {
// here i would like to manage errors which is 'undefined' type
}
catch(e if typeof e === "string") {
// here i can manage all string exeptions
}
// and so on and so on
catch(e) {
// and finally here i can manage another exeptions
}
finally {
// and a simple finally block
}
This is the same as we have in C#
or in a Java
.
Thanks in advance!
javascript
Is it possible to use multiple catch in JS(ES5 or ES6)
like I describe below (it is only example):
try {
// just an error
throw 1;
}
catch(e if e instanceof ReferenceError) {
// here i would like to manage errors which is 'undefined' type
}
catch(e if typeof e === "string") {
// here i can manage all string exeptions
}
// and so on and so on
catch(e) {
// and finally here i can manage another exeptions
}
finally {
// and a simple finally block
}
This is the same as we have in C#
or in a Java
.
Thanks in advance!
javascript
javascript
edited Nov 18 '15 at 14:16
ozil
4,36751741
4,36751741
asked Nov 18 '15 at 13:51
user5548116
1
One catch block and then check the type of the exception?
– Icepickle
Nov 18 '15 at 13:53
@Icepickle yeap you are right
– user5548116
Nov 18 '15 at 13:54
add a comment |
1
One catch block and then check the type of the exception?
– Icepickle
Nov 18 '15 at 13:53
@Icepickle yeap you are right
– user5548116
Nov 18 '15 at 13:54
1
1
One catch block and then check the type of the exception?
– Icepickle
Nov 18 '15 at 13:53
One catch block and then check the type of the exception?
– Icepickle
Nov 18 '15 at 13:53
@Icepickle yeap you are right
– user5548116
Nov 18 '15 at 13:54
@Icepickle yeap you are right
– user5548116
Nov 18 '15 at 13:54
add a comment |
4 Answers
4
active
oldest
votes
No. That does not exist in JavaScript or EcmaScript.
You can accomplish the same thing with an if[...else if]...else
inside of the catch
.
There are some non-standard implementations (and are not on any standard track) that do have it according to MDN.
I see, just using the one catch block and then i can check exception with an exception type. Thanks!
– user5548116
Nov 18 '15 at 13:59
add a comment |
Try in a that way:
try {
throw 1;
}
catch(e) {
if (e instanceof ReferenceError) {
// ReferenceError action here
} else if (typeof e === "string") {
// error as a string action here
} else {
// General error here
}
}
finally {}
add a comment |
This Kind of Multiple Catch we call in javascript as Conditional catch clauses
You can also use one or more conditional catch clauses to handle specific exceptions. In this case, the appropriate catch clause is entered when the specified exception is thrown. As below
try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
Non-standard:
But This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Reference
1
Thanks, this is Non-standard JS that's why i couldn't find anything with it
– user5548116
Nov 18 '15 at 14:04
add a comment |
There is absolutely nothing wrong with multiple if/then/else of course, but I never liked the look of it. I find that my eyes skim a bit faster with everything lined up, so I use the switch approach instead to help me skim/seek to the correct block. I've also started using a lexical scope {}
to enclose case blocks now that the ES6 let
command has gained popularity.
try {
// OOPS!
} catch (error) {
switch (true) {
case (error instanceof ForbiddenError): {
// be mean and gruff;
break;
}
case (error instanceof UserError): {
// be nice;
break;
}
default: {
// log, cuz this is weird;
}
}
}
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%2f33781818%2fmultiple-catch-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
No. That does not exist in JavaScript or EcmaScript.
You can accomplish the same thing with an if[...else if]...else
inside of the catch
.
There are some non-standard implementations (and are not on any standard track) that do have it according to MDN.
I see, just using the one catch block and then i can check exception with an exception type. Thanks!
– user5548116
Nov 18 '15 at 13:59
add a comment |
No. That does not exist in JavaScript or EcmaScript.
You can accomplish the same thing with an if[...else if]...else
inside of the catch
.
There are some non-standard implementations (and are not on any standard track) that do have it according to MDN.
I see, just using the one catch block and then i can check exception with an exception type. Thanks!
– user5548116
Nov 18 '15 at 13:59
add a comment |
No. That does not exist in JavaScript or EcmaScript.
You can accomplish the same thing with an if[...else if]...else
inside of the catch
.
There are some non-standard implementations (and are not on any standard track) that do have it according to MDN.
No. That does not exist in JavaScript or EcmaScript.
You can accomplish the same thing with an if[...else if]...else
inside of the catch
.
There are some non-standard implementations (and are not on any standard track) that do have it according to MDN.
edited Nov 18 '15 at 14:00
answered Nov 18 '15 at 13:53
Daniel A. WhiteDaniel A. White
149k36296375
149k36296375
I see, just using the one catch block and then i can check exception with an exception type. Thanks!
– user5548116
Nov 18 '15 at 13:59
add a comment |
I see, just using the one catch block and then i can check exception with an exception type. Thanks!
– user5548116
Nov 18 '15 at 13:59
I see, just using the one catch block and then i can check exception with an exception type. Thanks!
– user5548116
Nov 18 '15 at 13:59
I see, just using the one catch block and then i can check exception with an exception type. Thanks!
– user5548116
Nov 18 '15 at 13:59
add a comment |
Try in a that way:
try {
throw 1;
}
catch(e) {
if (e instanceof ReferenceError) {
// ReferenceError action here
} else if (typeof e === "string") {
// error as a string action here
} else {
// General error here
}
}
finally {}
add a comment |
Try in a that way:
try {
throw 1;
}
catch(e) {
if (e instanceof ReferenceError) {
// ReferenceError action here
} else if (typeof e === "string") {
// error as a string action here
} else {
// General error here
}
}
finally {}
add a comment |
Try in a that way:
try {
throw 1;
}
catch(e) {
if (e instanceof ReferenceError) {
// ReferenceError action here
} else if (typeof e === "string") {
// error as a string action here
} else {
// General error here
}
}
finally {}
Try in a that way:
try {
throw 1;
}
catch(e) {
if (e instanceof ReferenceError) {
// ReferenceError action here
} else if (typeof e === "string") {
// error as a string action here
} else {
// General error here
}
}
finally {}
edited Nov 26 '18 at 5:18
Shubham Khatri
83.2k15100140
83.2k15100140
answered Nov 18 '15 at 13:54
LegotinLegotin
886717
886717
add a comment |
add a comment |
This Kind of Multiple Catch we call in javascript as Conditional catch clauses
You can also use one or more conditional catch clauses to handle specific exceptions. In this case, the appropriate catch clause is entered when the specified exception is thrown. As below
try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
Non-standard:
But This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Reference
1
Thanks, this is Non-standard JS that's why i couldn't find anything with it
– user5548116
Nov 18 '15 at 14:04
add a comment |
This Kind of Multiple Catch we call in javascript as Conditional catch clauses
You can also use one or more conditional catch clauses to handle specific exceptions. In this case, the appropriate catch clause is entered when the specified exception is thrown. As below
try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
Non-standard:
But This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Reference
1
Thanks, this is Non-standard JS that's why i couldn't find anything with it
– user5548116
Nov 18 '15 at 14:04
add a comment |
This Kind of Multiple Catch we call in javascript as Conditional catch clauses
You can also use one or more conditional catch clauses to handle specific exceptions. In this case, the appropriate catch clause is entered when the specified exception is thrown. As below
try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
Non-standard:
But This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Reference
This Kind of Multiple Catch we call in javascript as Conditional catch clauses
You can also use one or more conditional catch clauses to handle specific exceptions. In this case, the appropriate catch clause is entered when the specified exception is thrown. As below
try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
Non-standard:
But This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Reference
answered Nov 18 '15 at 14:02
ANR Upgraded VersionANR Upgraded Version
656928
656928
1
Thanks, this is Non-standard JS that's why i couldn't find anything with it
– user5548116
Nov 18 '15 at 14:04
add a comment |
1
Thanks, this is Non-standard JS that's why i couldn't find anything with it
– user5548116
Nov 18 '15 at 14:04
1
1
Thanks, this is Non-standard JS that's why i couldn't find anything with it
– user5548116
Nov 18 '15 at 14:04
Thanks, this is Non-standard JS that's why i couldn't find anything with it
– user5548116
Nov 18 '15 at 14:04
add a comment |
There is absolutely nothing wrong with multiple if/then/else of course, but I never liked the look of it. I find that my eyes skim a bit faster with everything lined up, so I use the switch approach instead to help me skim/seek to the correct block. I've also started using a lexical scope {}
to enclose case blocks now that the ES6 let
command has gained popularity.
try {
// OOPS!
} catch (error) {
switch (true) {
case (error instanceof ForbiddenError): {
// be mean and gruff;
break;
}
case (error instanceof UserError): {
// be nice;
break;
}
default: {
// log, cuz this is weird;
}
}
}
add a comment |
There is absolutely nothing wrong with multiple if/then/else of course, but I never liked the look of it. I find that my eyes skim a bit faster with everything lined up, so I use the switch approach instead to help me skim/seek to the correct block. I've also started using a lexical scope {}
to enclose case blocks now that the ES6 let
command has gained popularity.
try {
// OOPS!
} catch (error) {
switch (true) {
case (error instanceof ForbiddenError): {
// be mean and gruff;
break;
}
case (error instanceof UserError): {
// be nice;
break;
}
default: {
// log, cuz this is weird;
}
}
}
add a comment |
There is absolutely nothing wrong with multiple if/then/else of course, but I never liked the look of it. I find that my eyes skim a bit faster with everything lined up, so I use the switch approach instead to help me skim/seek to the correct block. I've also started using a lexical scope {}
to enclose case blocks now that the ES6 let
command has gained popularity.
try {
// OOPS!
} catch (error) {
switch (true) {
case (error instanceof ForbiddenError): {
// be mean and gruff;
break;
}
case (error instanceof UserError): {
// be nice;
break;
}
default: {
// log, cuz this is weird;
}
}
}
There is absolutely nothing wrong with multiple if/then/else of course, but I never liked the look of it. I find that my eyes skim a bit faster with everything lined up, so I use the switch approach instead to help me skim/seek to the correct block. I've also started using a lexical scope {}
to enclose case blocks now that the ES6 let
command has gained popularity.
try {
// OOPS!
} catch (error) {
switch (true) {
case (error instanceof ForbiddenError): {
// be mean and gruff;
break;
}
case (error instanceof UserError): {
// be nice;
break;
}
default: {
// log, cuz this is weird;
}
}
}
answered Dec 30 '18 at 4:53
Geek StocksGeek Stocks
73421330
73421330
add a comment |
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%2f33781818%2fmultiple-catch-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
1
One catch block and then check the type of the exception?
– Icepickle
Nov 18 '15 at 13:53
@Icepickle yeap you are right
– user5548116
Nov 18 '15 at 13:54