Lesson 25 of 30 – JavaScript Error Handling
83%

JavaScript Error Handling

JavaScript is a loosely typed language. Sometimes runtime errors occur when accessing undefined variables or calling undefined functions.

Note: The try-catch block handles runtime errors but does not handle syntax errors.
What is Error Handling?

JavaScript provides an error-handling mechanism using:

  • try
  • catch
  • finally
  • throw

These statements help prevent programs from crashing when errors occur.

The try Block

The try block contains code that may generate an error.

Syntax:
try {
  // risky code
}

If an error occurs, JavaScript immediately transfers control to the catch block.

The catch Block

The catch block executes when an error occurs inside the try block.

Syntax:
try {
  // code
}
catch(error){
  // handle error
}
Example of try and catch
try{
var result = Sum(20,30);
}
catch(ex){
document.write(ex);
}

Since the function Sum() is not defined, an error occurs and is handled by the catch block.

Error Object

The parameter inside catch contains details about the error.

catch(ex){
document.write(ex);
}

You can display or log the error message using the error object.

The finally Block

The finally block always executes whether an error occurs or not.

Syntax:
try{
  // code
}
catch(ex){
  // error handling
}
finally{
  // always executes
}
Example of finally Block
try{
var result = Sum(20,20);
}
catch(ex){
document.write(ex);
}
finally{
document.write("Finally block executed");
}
Output:

Finally block executed

The finally block runs regardless of whether an error occurs.

Why Use finally?

The finally block is useful for cleanup operations.

  • Closing files
  • Resetting variables
  • Releasing resources
  • Displaying final messages
The throw Statement

The throw keyword is used to generate custom errors.

Syntax:
throw "Error Message";
Example of throw
try{
throw "Error occurred";
}
catch(ex){
alert(ex);
}
Output:

Error occurred

Custom Validation Example

You can use throw to validate user input.

var age = 15;

try{
if(age < 18){
throw "You must be 18 or older";
}
}
catch(ex){
document.write(ex);
}
Output:

You must be 18 or older

Common Runtime Errors
Error Type Description
ReferenceError Undefined variable or function
TypeError Invalid operation on a value
RangeError Number out of allowed range
URIError Invalid URI function usage
SyntaxError Invalid JavaScript syntax
Advantages of Error Handling
  • Prevents application crashes.
  • Improves user experience.
  • Makes debugging easier.
  • Allows custom error messages.
  • Improves program reliability.
  • Handles unexpected situations gracefully.
Points to Remember
  • JavaScript provides try, catch, finally and throw for error handling.
  • The try block contains risky code.
  • The catch block handles runtime errors.
  • The finally block always executes.
  • The throw statement creates custom errors.
  • try-catch cannot handle syntax errors.
  • Error handling improves program reliability.
  • Custom messages can be displayed using throw.
Quick Revision
Keyword Purpose
try Contains code that may generate errors
catch Handles errors
finally Always executes
throw Creates custom errors
Best Practices
  • Use try-catch around risky code.
  • Provide meaningful error messages.
  • Use finally for cleanup tasks.
  • Do not hide important errors.
  • Validate user input before processing.
  • Use custom errors when appropriate.

🧠 Quick Quiz

Which block always executes whether an error occurs or not?