Skip to content Skip to sidebar Skip to footer

How To Catch Exceptions In Javascript?

I want to catch exceptions in javascript if an insertion query is not done. I have written the code below: var adoConn = new ActiveXObject('ADODB.Connection'); var adoRS = new Acti

Solution 1:

To be complete, here's the full structure

try {
  // your code that can throw exception goes here
} catch(e) {
  //do stuff with the exception
} finally {
  //regardless if it worked or not, do stuff here (cleanup?)
}

Solution 2:

<scriptlanguage="JavaScript">try
{
 colours[2] = "red";
}
catch (e)
{
  alert("Oops! Something bad just happened. Calling 911...");
}

</script>

(Ripped from http://www.devshed.com/c/a/JavaScript/JavaScript-Exception-Handling/)

Solution 3:

try {
  // your code that can throw exception goes here
} catch(e) {
  //do stuff with the exception
}

FYI - the code you posted looks, well, for want of a better word, ugly! (No offense) Couldn't you use DWR or some other JavaScript framework (depending on your language choice) to hide all the DB connection stuff at the back end and just have the javascript calling the back end code and doing something with the response?

Solution 4:

try {
    adoConn.Execute("insert into session (SessionId,Timestamp) values ('"
                     + SessionId + "','" 
                     + SessionCurrenttime + "')");
} catch(e) {
    /*use error object to inspect the error: e.g. return e.message */
}

Post a Comment for "How To Catch Exceptions In Javascript?"