Skip to content

Instantly share code, notes, and snippets.

@ryu1-1uyr
Created April 2, 2019 17:42
Show Gist options
  • Save ryu1-1uyr/ee04e16dcb1b8c55ba92649c3c9de288 to your computer and use it in GitHub Desktop.
Save ryu1-1uyr/ee04e16dcb1b8c55ba92649c3c9de288 to your computer and use it in GitHub Desktop.
例外を使いたかった
function FizzException() {
this.name = "Fizz"
}
function BuzzException() {
this.name = "Buzz"
}
function FizzBuzzException() {
this.name = "FizzBuzz"
}
const throwSomeException = number => {
if (number % 15 == 0) {
throw new FizzBuzzException()
} else if (number % 3 == 0) {
throw new FizzException()
} else if (number % 5 == 0) {
throw new BuzzException()
} else {
return number
}
}
for (let i = 1; i < 31; i++) {
try {
console.log(throwSomeException(i))
} catch (e) {
console.log(e.name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment