Multiple catch in javascript












8















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!










share|improve this question




















  • 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


















8















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!










share|improve this question




















  • 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
















8












8








8








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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














4 Answers
4






active

oldest

votes


















6














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.






share|improve this answer


























  • 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



















2














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 {}





share|improve this answer

































    0














    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






    share|improve this answer



















    • 1





      Thanks, this is Non-standard JS that's why i couldn't find anything with it

      – user5548116
      Nov 18 '15 at 14:04



















    0














    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;
    }
    }

    }





    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      6














      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.






      share|improve this answer


























      • 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
















      6














      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.






      share|improve this answer


























      • 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














      6












      6








      6







      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.






      share|improve this answer















      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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



















      • 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













      2














      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 {}





      share|improve this answer






























        2














        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 {}





        share|improve this answer




























          2












          2








          2







          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 {}





          share|improve this answer















          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 {}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 5:18









          Shubham Khatri

          83.2k15100140




          83.2k15100140










          answered Nov 18 '15 at 13:54









          LegotinLegotin

          886717




          886717























              0














              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






              share|improve this answer



















              • 1





                Thanks, this is Non-standard JS that's why i couldn't find anything with it

                – user5548116
                Nov 18 '15 at 14:04
















              0














              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






              share|improve this answer



















              • 1





                Thanks, this is Non-standard JS that's why i couldn't find anything with it

                – user5548116
                Nov 18 '15 at 14:04














              0












              0








              0







              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






              share|improve this answer













              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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              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














              • 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











              0














              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;
              }
              }

              }





              share|improve this answer




























                0














                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;
                }
                }

                }





                share|improve this answer


























                  0












                  0








                  0







                  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;
                  }
                  }

                  }





                  share|improve this answer













                  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;
                  }
                  }

                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 30 '18 at 4:53









                  Geek StocksGeek Stocks

                  73421330




                  73421330






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks