Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0aea957

Browse files
committed
TODO: throw own exceptions
1 parent 9b0500b commit 0aea957

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

‎08-Control-Flow.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,115 @@ switch (browser) {
114114
```
115115

116116
# 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+
function divideNumbers(a, b) {
126+
try {
127+
if (b === 0) {
128+
throw new Error("Divide by zero");
129+
}
130+
const result = a / b;
131+
console.log(result);
132+
} catch (error) {
133+
console.error(error);
134+
return null;
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+
function divideNumbers(a, b) {
151+
try {
152+
if (b === 0) {
153+
throw new Error("Divide by zero");
154+
}
155+
const result = a / b;
156+
console.log(result);
157+
} catch (error) {
158+
console.error(error);
159+
return null;
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+
const gravity = 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+
const takeMe = decodeURIComponent("%"); //URIError
222+
```
223+
224+
- EvalError: This error is thrown when passing a string containing invalid JavaScript code to the `eval()` function.
225+
226+
```js
227+
eval("consolr.logg(x)"); //EvalError
228+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /