Skip to content

Commit ee36501

Browse files
committed
Recover deleted 14_day_Error_Handling file
1 parent 1df4102 commit ee36501

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<div align="center">
2+
<h1> 30 Days Of JavaScript: Error handling</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](../13_Day_Console_object_methods/13_day_console_object_methods.md) | [Day 15>>](../15_Day_Classes/15_day_classes.md)
18+
19+
![Thirty Days Of JavaScript](../images/banners/day_1_14.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 it will be executed
84+
```
85+
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.
86+
```js
87+
try {
88+
let lastName = 'Yetayeh'
89+
let fullName = fistName + ' ' + lastName
90+
} catch (err) {
91+
console.log('Name of the error', err.name)
92+
console.log('Error message', err.message)
93+
} finally {
94+
console.log('In any case I will be executed')
95+
}
96+
```
97+
```sh
98+
Name of the error ReferenceError
99+
Error message fistName is not defined
100+
In any case I will be executed
101+
```
102+
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:
103+
```js
104+
throw 'Error2' // generates an exception with a string value
105+
throw 42 // generates an exception with the value 42
106+
throw true // generates an exception with the value true
107+
throw new Error('Required') // generates an error object with the message of Required
108+
```
109+
```js
110+
const throwErrorExampleFun = () => {
111+
let message
112+
let x = prompt('Enter a number: ')
113+
try {
114+
if (x == '') throw 'empty'
115+
if (isNaN(x)) throw 'not a number'
116+
x = Number(x)
117+
if (x < 5) throw 'too low'
118+
if (x > 10) throw 'too high'
119+
} catch (err) {
120+
console.log(err)
121+
}
122+
}
123+
throwErrorExampleFun()
124+
```
125+
### Error Types
126+
- ReferenceError: An illegal reference has occurred. A ReferenceError is thrown if we use a variable that has not been declared.
127+
```js
128+
let firstName = 'Asabeneh'
129+
let fullName = firstName + ' ' + lastName
130+
console.log(fullName)
131+
```
132+
```sh
133+
Uncaught ReferenceError: lastName is not defined
134+
at <anonymous>:2:35
135+
```
136+
- SyntaxError: A syntax error has occurred
137+
```js
138+
let square = 2 x 2
139+
console.log(square)
140+
console.log('Hello, world")
141+
```
142+
```sh
143+
Uncaught SyntaxError: Unexpected identifier
144+
```
145+
- TypeError: A type error has occurred
146+
```js
147+
let num = 10
148+
console.log(num.toLowerCase())
149+
```
150+
```sh
151+
Uncaught TypeError: num.toLowerCase is not a function
152+
at <anonymous>:2:17
153+
```
154+
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.
155+
🌕 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.
156+
## Exercises
157+
### Exercises:Level 1
158+
Practice
159+
### Exercises: Level 2
160+
Practice
161+
### Exercises:Level
162+
Practice
163+
🎉 CONGRATULATIONS ! 🎉
164+
[<< Day 13](../13_Day_Console_object_methods/13_day_console_object_methods.md) | [Day 15>>](../15_Day_Classes/15_day_classes.md)

0 commit comments

Comments
 (0)