-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception-handling.js
80 lines (60 loc) · 1.85 KB
/
exception-handling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* exception handling */
/* 1.TRY:- this statement is used to test a block of codes for errors.
2.CATCH:- this statement is used to handle the error.
3.THROW. this statement is ussed to create custom error.
4.FINALLY:- this statement is used to execute code after TRY and CATCH regardless of the result.*/
//to handle the error
//convert error into language
/*use tryandcatch block to handle error:-
1.first try block will always execute, when error occurs catch will execute. */
//syntax
/*
try{
//statements;
}catch(arguments){
//statements;
}
*/
//example without try block error
/*
try{
console.log("Try block");
}catch(e){
console.log(`catch block error: ${e}`);
}
*/
//example with error
/*
try{
variable; //variable not defined
console.log("Try block");
}catch(e){
console.log(`catch block error: ${e}`);
}
*/
//example using error object
/*
try{
variable;
console.log("Try block"); //skip remaining statements
}catch(e){
console.log(`catch block error: ${e}`);
console.log(e.message);
console.log(e.name);
console.log(e.stack);
}
*/
//example for executing statements after the catch block executes-using finally block
/*
try{
variable;
console.log("Try block"); //skip remaining statements
}catch(e){
//console.log(`catch block error: ${e}`);
console.log(e.message);
console.log(e.name);
console.log(e.stack);
}finally{
console.log("finally executed");
}
*/