forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry_catch.js
More file actions
74 lines (68 loc) · 2.17 KB
/
Copy pathtry_catch.js
File metadata and controls
74 lines (68 loc) · 2.17 KB
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
/*
The try statement lets you test a block of code for errors.
The catch statement lets you handle the error.
The throw statement lets you create custom errors.
The finally statement lets you execute code, after try and catch, regardless of the result.
*/
/*
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
*/
console.log("pkp");
try {
addalert("Piyush priyadarshi");
} catch (err) {
console.log(err);
/*
ReferenceError: addalert is not defined
at Object.<anonymous> (F:\piyush\HacktoberFest_2019\Learn-JavaScript\JavaScript_Advance\tryCatch.js:22:3)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
*/
}
message = document.getElementsByTagName("input");
message.innerHTML = "";
input = document.getElementsByTagName("input").value;
try {
if (input == "") {
// if the input is empty then throw empty error
throw "empty";
}
if (isNaN(input)) {
// if the input is Not a Number then throw not a number error
throw "not a number";
}
x = Number(input);
if (x < 5) {
// if the input is less than 5 then throw too low error
throw "Too low";
}
if (x > 10) {
// if the input is greater than 10 then throw too high error
throw "Too high";
}
} catch (err) {
message.innerHTML = "Input is " + err;
} finally {
// Empty the input irrespective of the functionality
document.getElementsByTagName("input").value = "";
}
/*
Different types of the Error in JavaScript
EvalError An error has occurred in the eval() function
RangeError A number "out of range" has occurred
ReferenceError An illegal reference has occurred
SyntaxError A syntax error has occurred
TypeError A type error has occurred
URIError An error in encodeURI() has occurred
*/