Answer the question
In order to leave comments, you need to log in
How to create custom errors in JS?
I have a point class, I need to create exceptions for it. Exceptions will appear if not numbers come into its arguments and if not 2 but more or less arguments come.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
function isNum(num){
if(typeof num.x !== Number || typeof num.y !== Number ){
throw new Error('point is not a number ')
}
console.log('isnm')
}
let exampleNum = new Point(1,1);
console.log(exampleNum)
try{
isNum(exampleNum)
}catch(e){
console.log("Something went wrong")
console.error(e)
}
Answer the question
In order to leave comments, you need to log in
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
isNum()
{
if(typeof this.x !== 'number' || typeof this.y !== 'number' ) {
throw new Error('point is not a number ')
}
}
}
try{
let exampleNum = new Point(1,2)
exampleNum.isNum()
} catch(e){
console.error(e)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question