-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.js
More file actions
116 lines (99 loc) · 4.15 KB
/
errors.js
File metadata and controls
116 lines (99 loc) · 4.15 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//ReferenceError occurs when a variable has not been defined but you attempt to use it in your code
//An error is a faulty piece of code that prevents the program from further execution
//Syntax error occurs when you write a piece of code that javascript cannot read - var greet="hello
//A TypeError is thrown when, for example, trying to run a method on a non-supported data type. - (5).pop
//try and catch keywords can be used to try a block of code and possibly catch any errors if there is one
try {
//block of code goes here
} catch (err) {
console.log(err);
}
console.log("This line runs");
//including the "throw new Error" in the try and catch block can help to force an error to be thrown from the try block to the catch block which then stops the execution of other parts of the code immediately.
//The throw keyword can be used outside the try block but it will not be possible to catch the errors
try {
//block of code goes here
throw new Error();
} catch (err) {
console.log(err);
}
console.log("This line runs");
try {
console.log(a + b);
} catch (err) {
console.log(err);
console.log("There was an error");
console.log("There error was served in the error log");
}
console.log("my program does not stop");
//The benefit of using a try and catch block is that even if javascript experiences an error during execution, it will not stop executing other pieces of code
//null represents the intentional absence of any object value
//The undefined data type can only hold one value, undefined
//the undefined word is displayed after a console.log message because the console.log is a function and unless a function has some return value, it will always display undefine after executing its job
//Also the undefined word appears when a variable is defined without an assignment
var noise; //returns undefined
//Trying to access an object property that does not exist will return undefined
var game = {
score: 40,
};
console.log(game.Score); //returns undefined
//The empty value is a string with no characters between them
var empty1 = "";
//Exercise on error handling
function addTwoNums(a, b) {
try {
if (typeof a != "number") {
throw new ReferenceError("The first argument is not a number");
} else if (typeof b != "number") {
throw new ReferenceError("The second argument not a number");
} else {
console.log(a + b);
}
} catch (err) {
console.log("ERROR!", err);
}
}
addTwoNums(5, "5");
//Defensive programming
//Defensive programming is all about assuming that all the arguments a function will receive are of the wrong type, the wrong value or both.
function letterFinder(word, match) {
var condition1 = typeof word == "string" && word.length >= 2; //if the word is a string and the length is greater than or equal to 2
var condition2 = typeof match == "string" && match.length == 1; //if the match is a string and the length is equal to 1
if (condition1 && condition2) {
//if both condition matches
for (var i = 0; i < word.length; i++) {
if (word[i] == match) {
//if the current character at position i in the word is equal to the match
console.log("Found the", match, "at", i);
} else {
console.log("---No match found at", i);
}
}
} else {
console.log("Please pass in the correct arguments to the function");
}
}
letterFinder(1, 2);
letterFinder("cat", "c");
//Default params allows yoy to build a function that will run with default argument values even if you don't pass it any arguments while still being flexible enough to allow you to pass custom argument values and deal with them accordingly.
function withDefaultParams(number = 10) {
console.log('Result:', number * number)
}
class WithDefaultParams {
constructor(num1 = 1, num2 = 2, num3 = 3, string1 = "Result:", bool1 = true) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
this.string1 = string1;
this.bool1 = bool1;
}
calculate() {
if(this.bool1) {
console.log(this.string1, this.num1 + this.num2 + this.num3);
return;
}
return "The value of bool1 is incorrect"
}
}
var better = new WithDefaultParams();
better.calculate(); // Result: 6