SQL PHP HTML ASP JavaScript articles and free scripts to download
 

Handling error by try & catch in different browsers

We can use throw to pass some error conditions and monitor the program flow within a script. You can see how try catch is used to manage error handling at various stages of a program. The error message or any fixed message can be generated under different condition and can be collected by using catch.



Here is an example of throw command used under two different conditions. You can change the value of variable my_sum and see the difference in error messages.



<script type="text/javascript">
<!--
try
{
var my_sum=5;
document.write("The sum of 2 and 3 = " + my_sum);
if(my_sum != 5 )
throw "error1";
else if (my_sum==5)
throw "error2";
}
catch(err)
{
if(err=="error1")
document.write("<br>Sum of 2 and 3 has to be equal to 5");
else if(err=="error2")
document.write("<br>Correct the Sum of 2 and 3 has to be equal to 5");
else
document.write("There is some different error");
}
//-->
</script>


Further readings
For Loop with break statement
Do While Loop with break statement in JavaScript
Switch statement
If Else
Try Catch to handle run time errors in different browsers
throw to manage error message and program control
 
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
JavaScript Tutorials
Basic Loops
Popular Tutorials
Drop down list
Timer function
JavaScript Tutorials
String
Array
Date & Time
Form Validation
Event Handling
Math Functions
Loops & structure
JavaScript Forum
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.