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
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
+
thrownewerror("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
+
functionerror(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
+
leti=1;
33
+
while(i<=5){
34
+
try{
35
+
if(i%2!=0){
36
+
thrownewError("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
0 commit comments