Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active June 1, 2019 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrisonmalone/9651575b2b1dee4be4ff86ea18c77c45 to your computer and use it in GitHub Desktop.
Save harrisonmalone/9651575b2b1dee4be4ff86ea18c77c45 to your computer and use it in GitHub Desktop.
lecture code from try, catch and throw m0119
// 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