views:

96

answers:

4

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?

+4  A: 

See the try...catch statement on MDC.

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 if condition) { // Mozilla extension; optional
    // If exception thrown which matches condition
    // in try block, execute this block
} 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)).

strager
Thanks, I think this is making more sense now. So the parenthesis after catch holds the variable that is the errors?
chromedude
@srmorriso, Yes; see my edit.
strager
ok, thanks. Now it makes sense
chromedude
@strager, the catch identifier doesn't really need to be unique (I think that's JSLint stuff), when the `catch` statement runs, the scope is augmented, an object that contains a property named like the identifier is added to the scope chain, if another identifier existed in scope with the same name, it will be simply shadowed inside the `catch` block.
CMS
@CMS, Okay, maybe it was JSLint telling me things. (I use JSLint in my build process.) I remember something erroring when I had two try/catch statements and the catch statements had the same variable name. I'll edit my post to exclude that statement, though, as it's not really necessary.
strager
+1  A: 

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);
}
Johannes Wachter
so you don't necessarily need to have the finally statement?
chromedude
yes finally is an optional part of the statement that guarantees that the code inside it is executed regardless there was an exception or not.
Johannes Wachter
+1  A: 

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);
}
Crayon Violent
thanks, thats exactly what I was trying to find out
chromedude
A: 

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.

polemon