• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 6 pages
Other

Error Handling in JavaScript: A Beginner’s Guide with Examples

Document preview thumbnail
Preview 2 out of 6 pages

This document covers error handling in JavaScript, including how to use try-catch blocks, throwing exceptions, and working with JavaScript error objects. Learn how to catch and handle errors to write more reliable and robust code.

Content preview

Error Handling in JavaScript
Error handling in JavaScript allows developers to manage unexpected conditions
in a program gracefully, ensuring that the application continues running smoothly
and providing useful feedback to the user.

1. Types of Errors
JavaScript has several types of errors, which can be broadly categorized as:

 Syntax Errors: Occur when the code violates JavaScript's grammar rules.

const name = "John' // SyntaxError: Unexpected token

 Reference Errors: Occur when trying to reference a variable that is not
defined.

console.log(nonExistentVar); // ReferenceError: nonExistentVar is not
defined

 Type Errors: Occur when a value is not of the expected type.

null.foo(); // TypeError: Cannot read property 'foo' of null

 Range Errors: Occur when a value is out of range.

let numbers = new Array(-1); // RangeError: Invalid array length

 Eval Errors: Occur when using the eval() function inappropriately.

eval('alert("test")'); // EvalError: Invalid or illegal use of eval()



2. Try...Catch
The try...catch statement allows you to test a block of code for errors and handle
those errors gracefully.

, Syntax:

try {
// Code that may throw an error
} catch (error) {
// Code that handles the error
}

Example:

try {
const result = someUndefinedFunction();
} catch (error) {
console.log("An error occurred: " + error.message);
}

 try block: Contains the code that may cause an error.
 catch block: Contains the code that handles the error, typically logging the
error message or providing a fallback behavior.
 The catch block receives an Error object, which has properties like name,
message, and stack.

Handling Specific Errors:

You can catch specific types of errors by checking the name property of the error
object.

try {
const result = someUndefinedFunction();
} catch (error) {
if (error instanceof ReferenceError) {
console.log("Reference Error: " + error.message);
} else {
console.log("An unexpected error occurred: " + error.message);
}
}

Document information

Uploaded on
January 27, 2025
Number of pages
6
Written in
2024/2025
Type
Other
Person
Unknown
$7.39

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions