-
Notifications
You must be signed in to change notification settings - Fork 5
/
20_error_handling.js
110 lines (103 loc) · 4.08 KB
/
20_error_handling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Error handling allows you to gracefully handle exceptions and continue the program's execution.
*/
function errorHandling() {
/**
* ========================================================
* Basic Try-Catch
* ========================================================
* The try-catch block allows you to execute code within the "try" block.
* If an exception occurs, the code inside the "catch" block gets executed.
*/
try {
// Code that could possibly throw an error goes here
const x = JSON.parse('{"name": "Ishtmeet"}');
} catch (e) {
// Code to execute if an exception occurs in the try block
console.error("An error occurred:", e);
}
/**
* ========================================================
* The Finally Block
* ========================================================
* The "finally" block will execute after both the try and catch blocks, regardless of whether an exception was thrown.
* It's often used for cleanup tasks like releasing resources.
*/
try {
// Code that could possibly throw an error
} catch (e) {
// Code to execute if an exception occurs
} finally {
// This code will execute regardless of whether an exception occurred or not
console.log("Finally block executed");
}
/**
* ========================================================
* Throwing Exceptions
* ========================================================
* You can programmatically throw exceptions using the 'throw' keyword.
* Thrown exceptions can be of any type, but using the Error object is often more descriptive.
*/
try {
throw new Error("This is a custom error");
} catch (e) {
console.error("Caught a custom error:", e);
}
/**
* ========================================================
* Nuance: Catching Multiple Error Types
* ========================================================
* In complex applications, multiple types of errors can occur.
* You can use 'instanceof' or other properties to distinguish between them.
*/
try {
// Code that could throw multiple types of exceptions
throw new SyntaxError("A syntax error for demonstration purposes");
} catch (e) {
if (e instanceof TypeError) {
console.error("Type Error:", e);
} else if (e instanceof SyntaxError) {
console.error("Syntax Error:", e);
} else {
console.error("Unknown error:", e);
}
}
/**
* ========================================================
* Nuance: Catch Without Variable
* ========================================================
* JavaScript allows catch blocks without specifying the error variable.
* However, this isn't recommended as you lose the error details, making debugging difficult.
*/
try {
throw new Error("Some error");
} catch {
console.error("An error occurred, but we're not displaying it");
}
/**
* ========================================================
* Nuance: Optional Catch Binding
* ========================================================
* ECMAScript 2019 onwards permits the use of catch blocks without an error variable.
* While syntactically valid, this is generally not advised unless you have a very specific reason.
*/
try {
// some code that might throw an error
} catch {
// handle the error without capturing its details
}
/**
* ========================================================
* Bonus: Re-Throwing Errors
* ========================================================
* Sometimes, you might catch an error to log or modify it, but then want the error to propagate up.
* You can "re-throw" the error after catching it.
*/
try {
throw new Error("An error to be re-thrown");
} catch (e) {
console.error("Logging the error before re-throwing:", e);
throw e; // Re-throw the error
}
}
errorHandling();