Skip to content

Commit 543b7e4

Browse files
Merge pull request LaunchCodeEducation#15 from LaunchCodeEducation/exceptions
exceptions exercises and chapter examples
2 parents a1371a5 + 4623be8 commit 543b7e4

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const input = require('readline-sync');
2+
3+
let animals = [{name: 'cat'}, {name: 'dog'}];
4+
let index = Number(input.question("Enter index of animal:"));
5+
6+
try {
7+
console.log('animal at index:', animals[index].name);
8+
} catch(TypeError) {
9+
console.log("We caught a TypeError, but our program continues to run!");
10+
} finally {
11+
console.log("You tried to access an animal at index:", index);
12+
}
13+
14+
console.log("the code goes on...");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "control-flow-type-error-finally",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"readline-sync": "^1.4.10"
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw Error('You cannot divide by zero!');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "control-flow-type-error",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"readline-sync": "^1.4.10"
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const input = require('readline-sync');
2+
3+
let animals = [{name: 'cat'}, {name: 'dog'}];
4+
let index = Number(input.question("Enter index of animal:"));
5+
6+
try {
7+
console.log('animal at index:', animals[index].name);
8+
} catch(TypeError) {
9+
console.log("We caught a TypeError, but our program continues to run!");
10+
console.log("You tried to access an animal at index:", index);
11+
}
12+
13+
console.log("the code goes on...");

exceptions/exercises/divide.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Write a function called 'divide' that takes two parameters: a numerator and a denominator.
2+
3+
// Your function should return the result of numerator / denominator.
4+
5+
// However, if the denominator is zero you should throw the error, "Attempted to divide by zero."
6+
7+
// Code your divide function here:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function gradeLabs(labs) {
2+
for (let i=0; i < labs.length; i++) {
3+
let lab = labs[i];
4+
let result = lab.runLab(3);
5+
console.log(`${lab.student} code worked: ${result === 27}`);
6+
}
7+
}
8+
9+
let studentLabs = [
10+
{
11+
student: 'Carly',
12+
runLab: function (num) {
13+
return Math.pow(num, num);
14+
}
15+
},
16+
{
17+
student: 'Erica',
18+
runLab: function (num) {
19+
return num * num;
20+
}
21+
}
22+
];
23+
24+
gradeLabs(studentLabs);

0 commit comments

Comments
 (0)