Error Handling in Javascript
I’ve covered most everything you need to know to program in Javascript except for Error Handling. So, in this article I’ll explain how to handle those nasty errors when they crop up.
An exception is thrown whenever an error occurs in your code. You can set up your code in a way, so that it throws exceptions that you have a pre-built solution for. Let us say one of your functions divides numbers. We all know that dividing into zero produces a result of infinite. So, it would be a good idea to head off a zero division. How would you do that?
You would write some code that throws the exception or error and then catch and respond to such an error. The throw statement follows this style: throw expression;
Normally, the value of the expression is a string that represents what error caused an exception to be thrown. Ex. throw new error(“A division by zero occured”); Continue reading Javascript Scripting Tutorial Pt 5