In this article you will Learn how to handle error in
java script.
JavaScript is scripting language used for client side scripting. and error are
handling in two ways.
*try...catch statement
*onerror event
Try...Catch Statement
The try...catch statement grants an exception in a statement block to be captured
and handled. Remember one thing when we write try catch statement. try
catch statement should be written in lowercase if we write in upper case program
will return error. the try block contains run able code and catch block
contains the executable code
Syntax:
|
try |
| <html> <head> <script language="javascript"> function getweekName (wo) { wo=wo-1; // Adjust week number for array index (1=sunday, 7=Saturday) var week=new Array("sunday","monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); if (week[wo] != null) { return week[wo] } else { throw "InvalidweekNo" } } try { // statements to try weekName=getweekName(myweek) // function could throw exception } catch (e) { weekName="unknown" //logMyErrors(e) // pass exception object to error handler alert(e); } </script> </head> </body> <html> |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Error Handling in Java Script View All Comments
Post your Comment