Skip to content

Commit 3735449

Browse files
committed
day 14
1 parent 4648a74 commit 3735449

File tree

5 files changed

+2225
-1
lines changed

5 files changed

+2225
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ day9.md
1414
day10.md
1515
01_02_03_days_backup.md
1616
test.md
17-
14_Day
1817
15_Day
1918
16_Day
2019
17_Day

14_Day/14_day_error_handling.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<div align="center">
2+
<h1> 30 Days Of JavaScript</h1>
3+
<a class="header-badge" target="_blank" href="https://www.linkedin.com/in/asabeneh/">
4+
<img src="https://img.shields.io/badge/style--5eba00.svg?label=LinkedIn&logo=linkedin&style=social">
5+
</a>
6+
<a class="header-badge" target="_blank" href="https://twitter.com/Asabeneh">
7+
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/asabeneh?style=social">
8+
</a>
9+
10+
<sub>Author:
11+
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
12+
<small> January, 2020</small>
13+
</sub>
14+
15+
</div>
16+
17+
[<< Day 13](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/13_Day/13_day_console_object_methods.md) | [Day 15>>](#)
18+
19+
![Thirty Days Of JavaScript](../images/banners/day_1_15.png)
20+
21+
- [Day 14](#day-14)
22+
- [Error Handling](#error-handling)
23+
- [Error Types](#error-types)
24+
- [Exercises](#exercises)
25+
- [Exercises:Level 1](#exerciseslevel-1)
26+
- [Exercises: Level 2](#exercises-level-2)
27+
- [Exercises:Level](#exerciseslevel)
28+
29+
# Day 14
30+
31+
## Error Handling
32+
33+
JavaScript is a loosely-typed language. Some times you will get a runtime error when you try to access an undefined variable or call undefined function etc.
34+
35+
JavaScript similar to python or Java provides an error-handling mechanism to catch runtime errors using try-catch-finally block.
36+
37+
```js
38+
try {
39+
// code that may throw an error
40+
} catch (err) {
41+
// code to be executed if an error occurs
42+
} finally {
43+
// code to be executed regardless of an error occurs or not
44+
}
45+
```
46+
47+
**try**: wrap suspicious code that may throw an error in try block.The try statement allows us to define a block of code to be tested for errors while it is being executed.
48+
49+
**catch**: write code to do something in catch block when an error occurs. The catch block can have parameters that will give you error information. Catch block is used to log an error or display specific messages to the user.
50+
51+
**finally**: finally block will always be executed regardless of the occurrence of an error. The finally block can be used to complete the remaining task or reset variables that might have changed before error occurred in try block.
52+
53+
**Example:**
54+
55+
```js
56+
try {
57+
let lastName = 'Yetayeh'
58+
let fullName = fistName + ' ' + lastName
59+
} catch (err) {
60+
console.log(err)
61+
}
62+
```
63+
64+
```sh
65+
ReferenceError: fistName is not defined
66+
at <anonymous>:4:20
67+
```
68+
69+
```js
70+
try {
71+
let lastName = 'Yetayeh'
72+
let fullName = fistName + ' ' + lastName
73+
} catch (err) {
74+
console.error(err) // we can use console.log() or console.error()
75+
} finally {
76+
console.log('In any case I will be executed')
77+
}
78+
```
79+
80+
```sh
81+
ReferenceError: fistName is not defined
82+
at <anonymous>:4:20
83+
In any case I will be executed
84+
```
85+
86+
The catch block take a parameter. It is common to pass e, err or error as a parameter to the catch block. This parameter is an object and it has name and message keys. Lets use the name and message.
87+
88+
```js
89+
try {
90+
let lastName = 'Yetayeh'
91+
let fullName = fistName + ' ' + lastName
92+
} catch (err) {
93+
console.log('Name of the error', err.name)
94+
console.log('Error message', err.message)
95+
} finally {
96+
console.log('In any case I will be executed')
97+
}
98+
```
99+
100+
```sh
101+
Name of the error ReferenceError
102+
Error message fistName is not defined
103+
In any case I will be executed
104+
```
105+
106+
throw: the throw statement allows us to create a custom error. We can through a string, number, boolean or an object. Use the throw statement to throw an exception. When you throw an exception, expression specifies the value of the exception. Each of the following throws an exception:
107+
108+
```js
109+
throw 'Error2' // generates an exception with a string value
110+
throw 42 // generates an exception with the value 42
111+
throw true // generates an exception with the value true
112+
throw new Error('Required') // generates an error object with the message of Required
113+
```
114+
115+
```js
116+
const throwErroExampleFun = () => {
117+
let message
118+
let x = prompt('Enter a number: ')
119+
try {
120+
if (x == '') throw 'empty'
121+
if (isNaN(x)) throw 'not a number'
122+
x = Number(x)
123+
if (x < 5) throw 'too low'
124+
if (x > 10) throw 'too high'
125+
} catch (err) {
126+
console.log(err)
127+
}
128+
}
129+
throwErroExampleFun()
130+
```
131+
132+
### Error Types
133+
134+
- ReferenceError: An illegal reference has occurred. A ReferenceError is thrown if we use a variable that has not been declared.
135+
136+
```js
137+
let firstName = 'Asabeneh'
138+
let fullName = firstName + ' ' + lastName
139+
140+
console.log(fullName)
141+
```
142+
143+
```sh
144+
Uncaught ReferenceError: lastName is not defined
145+
at <anonymous>:2:35
146+
```
147+
148+
- SyntaxError: A syntax error has occurred
149+
150+
```js
151+
let square = 2 x 2
152+
console.log(square)
153+
154+
console.log('Hello, world")
155+
```
156+
157+
```sh
158+
Uncaught SyntaxError: Unexpected identifier
159+
```
160+
161+
- TypeError: A type error has occurred
162+
163+
```js
164+
let num = 10
165+
console.log(num.toLowerCase())
166+
```
167+
168+
```sh
169+
Uncaught TypeError: num.toLowerCase is not a function
170+
at <anonymous>:2:17
171+
```
172+
173+
These are some of the common error you may face when you write a code. Understanding errors can help you to know what mistakes you made and it will help you to debug your code fast.
174+
175+
🌕 You are flawless. Now, you knew how to handle errors and you can write robust application which handle unexpected user inputs. You have just completed day 14 challenges and you are 14 steps a head in to your way to greatness. Now do some exercises for your brain and for your muscle.
176+
177+
## Exercises
178+
179+
### Exercises:Level 1
180+
181+
Practice
182+
183+
### Exercises: Level 2
184+
185+
Practice
186+
187+
### Exercises:Level
188+
189+
Practice
190+
191+
🎉 CONGRATULATIONS ! 🎉
192+
193+
[<< Day 13](https://github.com/Asabeneh/30DaysOfJavaScript/blob/master/13_Day/13_day_console_object_methods.md) | [Day 15>>](#)

0 commit comments

Comments
 (0)