0
Throwing exception during object creation
Say, an object depends on some other object which must not be null and passed via constructor. Is throwing exceptions in a constructor without problems, or would it be ill-advised? 🤔
3 Respostas
+ 1
You may do that only if necessary (ex : the constructor needs a number as param and not an array, so if the param is an array, throw the exception)
0
Thank you for the response, VCoder ☺ If I understand correctly, then you object generally to throwing exceptions in a constructor 🤔 I was wondering, however, if you could be more specific about your reasoning behind your recommended practice ☺
0
In truth, you might do this in any method of the object like this
function divide(a, b) {
if(b == 0) throw "Division by 0 is impossible" // if executed, the program will stop
return a / b
}