Skip to content

Commit 1efe83a

Browse files
authored
Merge pull request #14 from SRedCodes/master
Error Handling using Try catch Finally block
2 parents 9dadc36 + 83c9d59 commit 1efe83a

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
*{
2+
padding: 0;
3+
margin: 0;
4+
box-sizing: border-box;
5+
}
6+
html,body{
7+
8+
width: 100%;
9+
font-size:16px;
10+
background-color: black;
11+
color:#fff;
12+
}
13+
main{
14+
height: 100vh;
15+
display: flex;
16+
flex-direction: column;
17+
justify-content: center;
18+
align-items: center;
19+
}
20+
h1{
21+
font-size: 250%;
22+
}

JavaScript/error handling/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>My Page</title>
7+
<link rel="stylesheet" href="css/main.css">
8+
<script src="js/main.js"></script>
9+
</head>
10+
<body>
11+
<main>
12+
<h1>My Page</h1>
13+
</main>
14+
</body>
15+
</html>

JavaScript/error handling/js/main.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
// Object..create(); // this is a SyntaxError
3+
4+
const makeError = ()=>{
5+
try{ // tries to excute code inside the try block and if found error , it'll be passed as parameter to catch
6+
// const name = "srikanth";
7+
// name = "bittu"; // this is a TypeError as const cannot be reassigned.
8+
throw new error("This is an error Message"); // throws a custom error
9+
10+
}catch (er){ // catches error from try block and displayes it
11+
console.log(er);
12+
console.error(er); // shows up in red background
13+
console.table(er); // shows up as table witg key-value pairs
14+
console.log(er.name); // provides name of the type of error like TypeError
15+
console.log(er.message); // provides explanation for the type of Error
16+
console.log(er.stack); // provides name + message
17+
// stack = name + message ... these are predefined parts for Error in JS
18+
} finally{
19+
console.log("...finally")
20+
}
21+
}
22+
makeError();
23+
function error(message){
24+
this.message = message
25+
this.name = "myError" ,
26+
this.stack = `${this.name} : ${this.message}`
27+
28+
}
29+
30+
//How does Try catch Finally block work with Example
31+
32+
let i = 1;
33+
while(i<=5){
34+
try{
35+
if(i%2 != 0){
36+
throw new Error("this is not an Even Number !");
37+
console.log("This is not reachable"); // after an error is thrown anything below that is not executed in a try block and it'll move straight to catch block
38+
}
39+
console.log(`${i} is a Even Number !`) // this is not executed when an error ius thrown
40+
}catch(error){
41+
console.error(error.stack);
42+
}finally{ // finally Block is executed everytime no matter what
43+
console.log(`${i} ...Finally`);
44+
i++;
45+
}
46+
}

0 commit comments

Comments
 (0)