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
// The try statement defines a code block to run (to try).
4
+
// The catch statement defines a code block to handle any error.
5
+
// The finally statement defines a code block to run regardless of the result.
6
+
// The throw statement defines a custom error.
7
+
8
+
// Errors Will Happen!
9
+
// When executing JavaScript code, different errors can occur.
10
+
// Errors can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things.
11
+
12
+
// In this example we misspelled "alert" as "adddlert" to deliberately produce an error:
13
+
14
+
// If we add try catch block the program run before and after error occurs. But not use try catch where an be possible to cause error next programm can not be run
15
+
16
+
17
+
console.log("Bismillah");
18
+
try{
19
+
addAlert("Welcome Guest!");
20
+
}catch(err){
21
+
console.log(err.message);
22
+
}
23
+
24
+
console.log("Hi thank you");
25
+
26
+
27
+
// The throw Statement
28
+
// The throw statement allows you to create a custom error.
29
+
// Technically you can throw an exception (throw an error).
30
+
// The exception can be a JavaScript String, a Number, a Boolean or an Object:
31
+
32
+
letinput=prompt("Enter a Number: ");
33
+
34
+
try{
35
+
if(input<18)throw"To Young";
36
+
elseif(input>100)throw"You do not need to vote! Rest in House";
37
+
}catch(err){
38
+
console.log(err);
39
+
}
40
+
41
+
// The finally Statement
42
+
// The finally statement lets you execute code, after try and catch, regardless of the result:
43
+
44
+
45
+
// Syntax
46
+
// try {
47
+
// Block of code to try
48
+
// }
49
+
// catch(err) {
50
+
// Block of code to handle errors
51
+
// }
52
+
// finally {
53
+
// Block of code to be executed regardless of the try / catch result
0 commit comments