It is very difficult to analyze all the situations to arise during runtime of a program because of which some conditions may remain unanswered, these unanswered conditions are called exceptions, JavaScript provides a facility to handle such situations. We can use try and catch statements to handle such situations. The whole code is kept inside the try block an in case any exception arises it is thrown using throw statement which is caught by the catch statement. |
Example |
<html>
<head> <title>events </title> </head> <body bgcolor=blue text=”white” > <h4> Enter Week Day Please</h4> Value 1: <input type=”text” id=”t1″ > <br> Value 1: <input type=”button” id=”b1″ value=”Hit me ” onclick=”pp()”> <br> <script> function pp() { var c= parseInt(document.all.t1.value)
try { switch(c) { case 1 : throw “Sunday” break case 2: throw “Monday” break case 3: throw “Tuesday” break case 4: throw “Wednesday” break case 5: throw “Thrusday” break case 6: throw “Friday” break case 7: throw “Saturday” break default: throw “The real error” } } catch(error) { alert(error) } } </script> </body> </html> |