Skip to content

Commit c3cf7bf

Browse files
committed
Added error handling
1 parent 65c0c09 commit c3cf7bf

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ This repository contains Vanilla JavaScript topics and their notes to learn from
8888
- [Async/Await](./docs/asynchronous-javascript.md#-async-await)
8989

9090
### Advanced Topics
91-
1. Error Handling:
92-
- Using `try...catch` blocks to handle errors
93-
- Custom error objects
91+
1. [Error Handling:](./docs/error-handling.md)
92+
- [try and catch](./docs/error-handling.md#-try-and-catch)
93+
- [Custom error objects](./docs/error-handling.md#-custom-error-objects)
94+
- [Necessity of Error Handling](./docs/error-handling.md#-necessity-of-error-handling)
9495
2. Regular Expressions:
9596
- Creating regular expressions
9697
- Common regular expression patterns

docs/error-handling.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## ⚑ Error Handling:
2+
This is a crucial aspect of JavaScript programming that involves anticipating and managing potential errors or exceptions that may occur during execution of the code. It helps to prevent unexpected crashes and stop execution flow of code. It provides a more robust and user-friendly experience in the application.
3+
4+
### ☴ Overview:
5+
1. [try and catch](#-try-and-catch)
6+
2. [Custom error objects](#-custom-error-objects)
7+
3. [Necessity of Error Handling](#-necessity-of-error-handling)
8+
9+
### ✦ try and catch:
10+
It is common error handling mechanism in JavaScript.
11+
12+
- *try:* It executes the code that may cause error.
13+
- *catch:* It executes the code that only error in the `try` block.
14+
15+
*Syntax:*
16+
```javascript
17+
try {
18+
// Code that might throw an error
19+
} catch (error) {
20+
// Code to be executed if an error occurs
21+
console.error(error);
22+
}
23+
```
24+
25+
```javascript
26+
function divide(a, b) {
27+
if (b === 0) {
28+
throw new Error("Division by zero");
29+
}
30+
return a / b;
31+
}
32+
33+
try {
34+
let result = divide(10, 0);
35+
console.log(result);
36+
} catch (error) {
37+
console.error(error.message);
38+
}
39+
```
40+
41+
### ✦ Custom error objects:
42+
This allows to create specific error types with custom messages and properties.
43+
44+
*Syntax:*
45+
```javascript
46+
function CustomError(message) {
47+
this.message = message;
48+
this.name = "Custom Error";
49+
}
50+
```
51+
52+
```javascript
53+
function validateAge(age) {
54+
if (price < 10) {
55+
throw new Error("Under Price");
56+
}
57+
if (age > 120) {
58+
throw new CustomError("Over Price");
59+
}
60+
}
61+
```
62+
63+
### &#10022; Necessity of Error Handling
64+
- *Prevent unexpected crashes:* It helps to avoid unexpected application failures and provides a more stable execution and experience.
65+
- *Provide informative error messages:* Custom error objects can provide informative and helpful error messages to users.
66+
- *Improve code reliability:* By anticipating potential errors and handling them, It makes code more robust, less prone to bugs and execution failure.
67+
- *Enable better debugging:* Error handling can helps to identify issue more efficiently.
68+
- *Enhance user experience:* Informative error messages can guide users to correct errors or prevent potential data loss.
69+
70+
---
71+
[&#8682; To Top](#-error-handling)
72+
73+
[&#10094; Previous Topic](./asynchronous-javascript.md) &emsp; [Next Topic &#10095;](./regular-expressions.md)
74+
75+
[&#8962; Goto Home Page](../README.md)

0 commit comments

Comments
 (0)