-
-
Save harrisonmalone/9651575b2b1dee4be4ff86ea18c77c45 to your computer and use it in GitHub Desktop.
lecture code from try, catch and throw m0119
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// what are try, catch and throw | |
// in try we execute some code | |
// to make a custom error we use throw | |
// throwing an error | |
// if an error occurs we then execute the code inside of catch | |
function CustomError(message) { | |
this.message = message | |
} | |
try { | |
const value = false | |
if (value != true) { | |
throw new CustomError("you got an error") | |
} else { | |
console.log("everything is okay") | |
} | |
} | |
catch(e) { | |
console.log(e.message) | |
} | |
const getAreaOfRectangle = (length, width) => { | |
if (typeof(length) !== "number" || typeof(width) !== "number") { | |
throw('the arguments you passed aren\'t numbers') | |
} else { | |
return length * width | |
} | |
} | |
try { | |
const result = getAreaOfRectangle("string", 2) | |
console.log(result) | |
} | |
catch(error) { | |
console.log(error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment