The title may not be the best.
Code sample:
a = 1;
try {
if (a == 1) {
throw ('My fake error.');
}
} catch (err) {
console.log(err.message);
}
Assume I have more complex code in the try
block. If a statement in that try
block throws an error I can get the text of the error by looking at err.message
. But, if I throw
an error myself I can only see the text if I look at err
because err.message
is undefined.
How do I catch both unexpected errors, and errors I throw
in the same catch block without writing more complex code to see if err.message
is null
for instance? I would expect both to return a similar structure, but they do not.