Home

Try/Catch/Throw - Error trapping

The try/catch/finally statement handles some or all of the errors that may occur in a block of code, while still running code.
Errors can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things.
The try statement allows you to define a block of code to be tested for errors while it is being executed.
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
The finally statement lets you execute code, after try and catch, regardless of the result.

The throw statement throws (generates) an error.

Note: The catch, finally & throw statements are both optional, but you need to use one of them (if not both) while using the try statement.

Try/Catch

Code placed inside the TRY area will be processed and if it errors out then there will be a message caught by the CATCH command.
The error message will be in the ERR variable.
You can also create your own error message via the THROW command, which will also get caught by the CATCH.
If you need some code to be run regardless of any error then you can use the FINALLY command to run that code.

Eg...
try {
   tryCode - Block of code to try
   ...and you throw your own 'error message' in here if needed (eg throw "my message here")

}
catch(err) {
   catchCode - Block of code to handle errors
}
finally {
   finallyCode - Block of code to be executed regardless of the try / catch result
}



NOTE
If using finally {} then it may be worth setting a variable inside the catch {} area so you can check it (in the finally {} bit) to see if an actual error occured or not.

Example 1...uses THROW to create error messages...
To test enter an invalid entry in the box.

Please input a number between 5 and 10:





Example 2...this time I will make a programming error and an then that will cause an error message without using THROW.

Please input a number between 5 and 10:




Note...
I want to be able to enter text in the input box(es) then when I press [Enter] it fire the relevant button's click event.
To do this I added the jQuery code (this shows code for the top button):-

$("#demo").keyup(function(event){
   if(event.keyCode == 13){
      $("#btn").click();
   }
});


And the second input box I programmed in DOM
by adding the onkeyup command to the input box and adding the function functionKU().
Just to show two different ways of doing it.

<input id="demo2" type="text"    onkeyup="functionKU(event)">

function functionKU(event){
   if(event.keyCode == 13) {
      document.getElementById('btn2').click();
   }
}

Top

You can also create your own actual error message that looks like a proper error (formatted like an error).
So when you send it to the console (console.log(Error("ownMessage")) it will give more info, eg...

var x = "This is my own created error message";
will display the following...

and console.log(x) will display as


While this will be different as it users the Error(...) command...
var x = Error("This is my own created error message");
and will display the following...

and console.log(x) will display as


Note... the line number (227) may change if I change the program

Therefore, if you use console.log when testing the program then this may be helpful.

So if you had the following code...

console.log('Started Program.');
throw Error('But wait there is an error.');
console.log('Ended Program');


When this was run you WILL GET 'Started Program' displayed to the console
BUT you will NOT get 'Ended Program' displayed as the throw Error stops it from running.

What you will see on the console.log is...
Started Program.
Uncaught Error: But wait there is an error.
at :2:7


the 'at :2:7' will probably be a different message as this could change in different programs.

Top