Error Handling in Java Script
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 |
Example:
The coming exercise uses a try...catch statement. The example calls a function that retrieves a week name from an array based on the value passed to the function. If the value does not correspond to a week number (1-7), an exception is thrown with the value InvalidWeekNo and the statements in the catch block set the weekName variable to unknown.
| <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> |
Tutorials
- JavaScript method doScroll()
- Popup Window Example in JavaScript
- JavaScript method deleteRow()
- JavaScript Variables and Data types
- Java Script Code of Calendar and Date Picker or Popup Calendar
- JavaScript - JavaScript Tutorial
- Looping In Java Script
- What is JavaScript? - Definition
- Conditional Examples(if - else- switch case) in JavaScript
- Classes-Objects in JavaScript
- String Number Operations in JavaScript
- Conditions In Java Script
- JavaScript - JavaScript Tutorial
- JavaScript Object Oriented Feature
- Form Validation using Regular Expressions is JavaScript
- Navigation with Combo box and Java Script
- Simple Calculator Application In Java Script
- JavaScript Combo Box Validation
- JavaScript createPopup method
- JavaScript appendChild method
- JavaScript add method
- JavaScript addImport example
- JavaScript appendData method
- JavaScript applyElement method Example
- JavaScript blink method
- JavaScript bold method
- JavaScript clear method
- JavaScript clearTimeOut method
- JavaScript click method
- JavaScript cloneNode example
- JavaScript createAttribute method
- JavaScript createCaption method
- JavaScript createComment method
- JavaScript createEventObject method
- JavaScript createTFoot method
- JavaScript createTHead method
- JavaScript deleteCaption method
- JavaScript deleteTFoot method
- JavaScript deleteTHead method
- JavaScript dragDrop method


