How javascript try…catch statement works

前端 未结 5 826
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 04:22

I am trying to test in browsermob if certain input field work or not. I am attempting to use a try...catch statement which I have never used before. I know that the form is:

5条回答
  •  眼角桃花
    2020-12-18 04:31

    See the “try...catch statement” guide on MDN.

    In short, try/catch is used to handle exceptions (which are "thrown" using the throw statement). The syntax for try/catch is:

    try {
        // Code
    } catch (varName) {              // Optional
        // If exception thrown in try block,
        // execute this block
    } finally {                      // Optional
        // Execute this block after
        // try or after catch clause
        // (i.e. this is *always* called)
    }
    

    varName is available to the scope of the catch block only. It refers to the exception object which was thrown (which could be any type of object, e.g. a String, but is usually an Error object).

提交回复
热议问题