Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5
Error handling in JavaScript
Error handling in JavaScript
• JavaScript is a loosely-typed language. • It does not give compile-time errors. • So some times you will get a runtime error for accessing an undefined variable or calling undefined function etc. • JavaScript provides error-handling mechanism to catch runtime errors using try-catch-finally block, similar to other languages like Java or C#. Error handling in JavaScript try { // code that may throw an error } catch(ex) { // code to be executed if an error occurs } finally{ // code to be executed regardless of an error occurs or not } Error handling in JavaScript try: wrap suspicious code that may throw an error in try block. catch: write code to do something in catch block when an error occurs. The catch block can have parameters that will give you error information. Generally catch block is used to log an error or display specific messages to the user. Error handling in JavaScript finally: code in the finally block will always be executed regardless of the occurrence of an error. The finally block can be used to complete the remaining task or reset variables that might have changed before error occurred in try block. throw: use throw keyword to raise a custom error.
JavaScript Fundamentals: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers
JavaScript Fundamentals: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers