You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 08-Control-Flow.md
+112Lines changed: 112 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -114,3 +114,115 @@ switch (browser) {
114
114
```
115
115
116
116
# Exception Handling
117
+
118
+
Exception Handling is the process of turning a error message into a user-friendly error message. When JavaScript finds an error, JavaScript interpreter looks for exception handling code rather than moving forward to next statement.
119
+
120
+
## Catching Exceptions
121
+
122
+
We can add an exception handler with `try...catch` block. The code in the `try` block gets executed first but when the exception occurs, the `catch` block is executed. This helps us to output the error message rather than unexpected termination of the program.
123
+
124
+
```js
125
+
functiondivideNumbers(a, b) {
126
+
try {
127
+
if (b ===0) {
128
+
thrownewError("Divide by zero");
129
+
}
130
+
constresult= a / b;
131
+
console.log(result);
132
+
} catch (error) {
133
+
console.error(error);
134
+
returnnull;
135
+
}
136
+
}
137
+
138
+
divideNumbers(12, 0); //OUTPUT: Error: Divide by zero
139
+
```
140
+
141
+
In this example, we're using `try` and `catch` to handle any errors that may occur when dividing two numbers. If the second number is 0, a divide-by-zero error will be thrown, which will cause the code inside the `catch` block to execute.
142
+
143
+
Inside the `catch` block, we're logging an error message to the console using `console.error` and returning null to indicate that the division failed. By using try-catch in this way, we can ensure that our function doesn't crash when it encounters an error, and instead handles the error gracefully.
144
+
145
+
**finally**
146
+
147
+
We can also define an optional `finally` block if we want to run specific code if either of `try` or `cacth` block is executed.
148
+
149
+
```js
150
+
functiondivideNumbers(a, b) {
151
+
try {
152
+
if (b ===0) {
153
+
thrownewError("Divide by zero");
154
+
}
155
+
constresult= a / b;
156
+
console.log(result);
157
+
} catch (error) {
158
+
console.error(error);
159
+
returnnull;
160
+
} finally {
161
+
console.log("Finished dividing numbers");
162
+
}
163
+
}
164
+
```
165
+
166
+
```
167
+
divideNumbers(12,2);
168
+
*OUTPUT:*
169
+
6
170
+
"Finished dividing numbers.
171
+
```
172
+
173
+
```
174
+
divideNumbers(12,0);
175
+
*OUTPUT:*
176
+
Error: Divide by 0
177
+
"Finished dividing numbers.
178
+
```
179
+
180
+
The code inside the `finally` block is executed regardless of whether an error was throw or not.
181
+
182
+
## JavaScript Error Types
183
+
184
+
- SyntaxError: We face such error when we make some mistake syntatically lilke spellings (e.g. funcion insted of function) or missing brackets.
185
+
186
+
```js
187
+
if condition) { //SyntaxError
188
+
conosle.log('hello)
189
+
}
190
+
```
191
+
192
+
- ReferencError: We face such error when we try to access non-existent variable.
193
+
194
+
```js
195
+
function doSomething() {
196
+
console.log(result); //ReferenceError
197
+
}
198
+
```
199
+
200
+
- TypeError: We face such error when the value is not of expected type.
201
+
202
+
```js
203
+
const num = 5;
204
+
const str = "hello";
205
+
206
+
const result = num + str; // TypeError: can't convert 'number' to string
207
+
```
208
+
209
+
- RangeError: We get this error when a value isn't in the set or range of allowed values.
210
+
211
+
```js
212
+
constgravity=9.81;
213
+
console.log(gravity.toFixed(-2)); //RangeError
214
+
```
215
+
216
+
Typically, `toFixed()` function expects a value to be between 0 and 100 but since we passed a negative number, a `RangeError` is thrown.
217
+
218
+
- URIError: These are the error thrown by URI-handling functions such as `encodeURI()` and `decodeURI()` when they encounter malformed URIs.
219
+
220
+
```js
221
+
consttakeMe=decodeURIComponent("%"); //URIError
222
+
```
223
+
224
+
- EvalError: This error is thrown when passing a string containing invalid JavaScript code to the `eval()` function.
0 commit comments