How javascript try…catch statement works

前端 未结 5 813
伪装坚强ぢ
伪装坚强ぢ 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).

    0 讨论(0)
  • 2020-12-18 04:33

    The try catch statement is used to detected for exceptions/errors that are raised inside the try-block. In the catch block you can then react on this exceptional behavior and try to resolve it or get to a safe state.

    You got the statement almost right:

    try {
     // code that may fail with error/exception
    } catch (e) { // e represents the exception/error object
     // react
    }
    

    Consider the following examples:

    try {
      var x = parseInt("xxx");
      if(isNaN(x)){
        throw new Error("Not a number");
      }
    } catch (e) { // e represents the exception/error object
     alert(e);
    }
    
    try {
     // some code
     if(!condition){
       throw new Error("Something went wrong!");
     }
    } catch (e) { // e represents the exception/error object
     alert(e);
    }
    
    0 讨论(0)
  • 2020-12-18 04:33

    the code that is likely to throw an exception goes into try { }, The code to be run when an exception is thrown, comes into catch() { }. In catch() you can specify which exceptions you want to catch, and in which automatic variables to put it. finally { } is always run, regardless whether exception was thrown or not.

    0 讨论(0)
  • 2020-12-18 04:37

    the stuff inside try {...} is what you want to execute. The stuff in catch() { ... } is what you want to execute if you get any javascript errors from anything executed in the try {...}

    catch {...} only executes if there is a javascript error in the try {...} block. You can find out what the error is by doing for example this:

    try {
     // do something 
    } catch (err) {
      alert(err);
    }
    
    0 讨论(0)
  • 2020-12-18 04:40

    According to ECMAScript specifications,

    try {
        // Code
    } catch (varName) {  // optional if 'finally' block is present.
      if (condition) {   // eg. (varName instanceof URIError)
        // Condition (Type) specific error handling
      }
      else {
        // Generic error handling
      }
    } finally {          // Optional if 'catch' block is present.
        // Execute this block after
        // try or after catch clause
        // (i.e. this is *always* called)
    }
    
    0 讨论(0)
提交回复
热议问题