Types OF Errors And Exceptions In Javascript

Hey, here we bring another awesome tutorial on errors and exceptions, where we will be discussing various types of errors in any programming language and when they occur. Let’s discuss the various types of errors and any relevant examples;
i) Runtime errors(exceptions)– These type of errors occurs when executing any program codes, for instance this error is returned when you call a function that does not exist, for better understanding we will have an example as follows;
<script type="text/javascript">
<!--
window.printme();
//-->
</script>

Since no parameters called in the brackets it will bring an error.

ii) Syntax Errors -They occur when compiling your codes, when they occur the only code contained in the same thread as syntax error is affected and the rest of the code is executed normally, for instance we may have this example where by a opening parenthesis is missing;

<script type="text/javascript">
<!--
window.print);
//-->
</script>

It will only bring error in that particular part where the parenthesis is missing.

iii) Logical Errors -They occur when your script codes outputs unexpected result may be due to a certain mistake made when writing the codes, this makes them hard to be identified;

Types of Error Handling

  • The oneerror() Method– It was the first feature to be discovered which handles errors in Javascript. We may have an example to illustrate the oneerror event handler;
<head>
<script type="text/javascript">
<!--
window.onerror = function () {
document.write ("An error has occurred.");
}
//-->
</script>
</head>
<body>

<form>
<input type="button" value="Click Me" onclick="myFunc();" />
</form>
</body>

It will produce a button which when clicked it will produce an error has occurred. 

-You can oneerror with many HTML tags to display appropriate messages in case of errors.
  • The throw Statement -This is another way of handling errors in Javascript, where it can be used to raise customized or built-in exceptions, later they can be captured and an appropriate action is taken. We may have this example to illustrate this type of error handling method;
<head>
<script type="text/javascript">
<!--
function myFunc()
{
var a = 150;
var b = 0;
try{
if ( b == 0 ){
throw( "Divide by zero error." );
}else{
var c = a / b;
}
}catch ( e ) {
document.write ("Error: " + e );
}
}
//-->
</script>
</head>
<body>
<form>
<input type="button" value="Click Me" onclick="myFunc();" />
</form>
</body>

You may use different values and try it yourself ad see what happens
  • The try…catch…finally Statement – It commonly applied in the latest versions of Javascript, whereby they implements the try…catch…finally construct and throw operator to handle the errors, they cannot catch javascript syntax errors but they can catch runtime errors; we may have it’s syntax as;
<script type="text/javascript">
<!--
try {
// Code to run
[break;]
} catch ( e ) {
// Code to run if an exception occurs
[break;]
}[ finally {
// Code that is always executed regardless of
// an exception occurring
}]
//-->
</script>

The try block should be followed by either finally or catch block or even both. For instance, when an exception occurs in the try block, then catch block is executed while the finally block executes unconditionally after try or catch. We may have an example to illustrate this;

<head>
<script type="text/javascript">
<!--
function myFunc()
{
var a = 120;
try {
document.write ("Value of variable a is : " + a );
} catch ( e ) {
document.write ("Error: " + e.description );
}
}
//-->
</script>
</head>
<body>

<form>
<input type="button" value="Click Here" onclick="myFunc();" />
</form>
</body>


You may try the example without the try...catch blocks and see what happens an error will occur depending on the type of browser.
With that hope you have understood various types of errors and how you can handle them, for the live sessions and other presentations in our program you subscribe to our YouTube channel here;YOUTUBE LINK, thanks for reading through our tutorial, hoping to see you in our next tutorial.