Articles → JavaScript → Exception Handling In Javascript

Exception Handling In Javascript






Software Requirements




  1. Windows any version is installed on your machine.
  2. Notepad or any text editor for writing code.

Prerequisite Knowledge




  1. Basic knowledge of JavaScript.
  2. Basic knowledge of HTML.
  3. Knowledge of any programming language is an added advantage.

What Are Exceptions?





Benefits Of Handling Exceptions





Try Catch In Javascript




try {
    //  Write your code here
} catch (e) {
    //  Write error handling code here
}






try {
    document.getElementById('aa').value = 'test value';
} catch (e) {
    alert(e);
}




Picture showing the alert box showing the exception caught in JavaScript

Click to Enlarge


Throw In Javascript






throw '<Exception_Message>';




Picture showing the cycle of throw keyword in exception handling in JavaScript

Click to Enlarge



try {
    alert('Before Throw');
    throw 'Error';
    alert('After Throw');
} catch (e) {
    alert(e);
}





Posted By  -  Karan Gupta
 
Posted On  -  Thursday, July 19, 2012

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250