Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How javascript try...catch statement works

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:

try {
//some code
} catch (){
//some error code
};

What exactly is supposed to be put in the parenthesis after the catch statement? When I try to use the statement it runs everything through the catch statement no matter if it is not an error. What am I doing wrong?

like image 391
chromedude Avatar asked Aug 14 '10 17:08

chromedude


People also ask

How does a try catch statement work?

Try defines a block of statements that may throw an exception. When a specific type of exception occurs, a catch block catches the exception. If an exception is not handled by try/catch blocks, the exception escalates through the call stack until the exception is caught or an error message is printed by the compiler.

Is try catch bad practice JavaScript?

try-catch in javascript is just as valid and useful as in any other language that implements them.

Can you return in a try catch JavaScript?

1. Return statement inside the try or catch block. If we have a finally block, the return statement inside try and catch block are not executed. It will always hit the finally block.

What happens if an error occurs in catch block JavaScript?

If an error is caused inside the try block then the execution control is transferred to catch block. In simple words, try checks for any error, and catch receives execution control when an error occurs and can handle it. Afterwards, the program is resumed as normal. If no error occurs, then the catch block is skipped.


2 Answers

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).

like image 74
4 revs, 2 users 86% Avatar answered Sep 20 '22 16:09

4 revs, 2 users 86%


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);
}
like image 24
Johannes Wachter Avatar answered Sep 19 '22 16:09

Johannes Wachter