Articles → JavaScript → Exception handling in JavaScript

Exception handling in JavaScript


















Software requirements









Prerequisite knowledge











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