Multiple catch in javascript

后端 未结 4 988
清歌不尽
清歌不尽 2021-01-04 00:34

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


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 01:21

    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

提交回复
热议问题