Error Handling
- This lesson introduces structured error handling techniques in JavaScript.
Introduction to Error Handling
In real-world JavaScript applications, errors are unavoidable.
Errors may occur due to:Incorrect user input
Network failure
Logical mistakes
Unexpected runtime situations
If errors are not handled properly:
The application may crash
Users may see blank screens
Data may be lost
Debugging becomes difficult
Error handling helps developers detect, manage, and respond to errors gracefully instead of letting the program fail.
What Is an Error ?
An error is a problem that occurs during the execution of a program which disrupts the normal flow of code.
In JavaScript, when an error occurs:
The current execution stops
Remaining code is not executed
An error message is generated
Simple Definition
Error handling is the process of identifying, controlling, and responding to errors in a program so that the application continues to run safely.
Why Error Handling Is Important
Error handling helps to:
Prevent application crashes
Show user-friendly messages
Improve application reliability
Make debugging easier
Handle unexpected situations
Real-Life Analogy
Consider an ATM machine:
If network fails, it shows a message instead of shutting down
If PIN is wrong, it allows retry
If cash is insufficient, it displays a warning
This controlled behavior is error handling.
Types of Errors in JavaScript (High-Level Overview)
JavaScript errors can broadly be categorized into:
Syntax Errors
Runtime Errors
Logical Errors
These will be discussed in detail in upcoming lessons.
Syntax Errors
Syntax errors occur when the code does not follow JavaScript rules.
Syntax Error Example
Missing closing parenthesis causes a syntax error.
if (x == 10 {
console.log("Hello");
}
Problem:
Missing closing parenthesis
Result:
Code will not run at all
Runtime Errors
Runtime errors occur while the program is running.
Type Error Example
Accessing a property on undefined causes a runtime error.
let x = undefined;
console.log(x.length);
Problem:
Cannot read property of undefined
Result:
Program crashes during execution
Logical Errors
Logical errors occur when:
Code runs without crashing
Output is incorrect
Logical Error Example
Incorrect logic causes wrong output in both conditions.
let marks = 35;
if (marks > 40) {
console.log("Pass");
} else {
console.log("Pass");
}
Problem:
Wrong condition logic
Result:
Incorrect output
What Happens When Errors Are Not Handled ?
Without proper error handling:
JavaScript stops execution
Remaining code is skipped
User sees broken functionality
Debugging becomes difficult
Example:
console.log("Start");
console.log(x);
console.log("End");
How JavaScript Handles Errors by Default
By default:
JavaScript throws an error
Displays error in console
Stops further execution
This default behavior is not suitable for production applications.
What Is Graceful Error Handling ?
Graceful error handling means:
Detecting errors early
Preventing app crashes
Showing meaningful messages
Allowing application to continue
Example message shown to users:
“Something went wrong. Please try again later.”
Common Situations Where Error Handling Is Needed
Form validation
API requests
File handling
JSON parsing
DOM manipulation
User input processing
Example: Error Without Handling
let data = JSON.parse("invalid json");
console.log("Data loaded");
Result:
Application crashes
Code stops
JSON Error Handling Example
Uses try-catch to handle errors when parsing invalid JSON.
try {
let data = JSON.parse("invalid json");
} catch (error) {
console.log("Invalid data format");
}
The program continues safely.
(Detailed try-catch will be covered in upcoming lessons.)
Error Handling vs Debugging
Both are important but serve different purposes.
Best Practices (Overview Level)
Always expect failure
Never trust user input
Handle external data carefully
Log errors properly
Avoid silent failures
Common Mistakes
Ignoring errors in console
Assuming code will always work
Not validating inputs
Not using error handling mechanisms
Letting application crash
Real-World Impact of Poor Error Handling
Poor user experience
App crashes
Security risks
High maintenance cost
Negative brand impression