Consider the following example:
In the example given above,
if the credentials match the expected values
we show a message using console.log
.
Even if the credentials are wrong,
we show a message using console.log
.
What if we want to show an error,
when the credentials are wrong?
In JavaScript,
we can show a custom error
using the throw
keyword.
In the example given above,
if the credentials are wrong
a custom error is thrown.
Let's wrap the example given above
in a try
block
and
see what happens if we access the
error inside the catch
block.
In the example given above,
we see that the custom error
is not an Error object.
It is a string.
To throw an Error object
we need to use new Error()
.