Creating an Exception
This lesson introduces how to work with exceptions.
We'll cover the following
Throwing exceptions
TypeScript follows ECMAScript in terms of the exception. You can throw a new exception by using the keyword throw
followed by an instance of the exception you desire. It’s always better to throw an object instead of a string. The reason is that throwing an object comes with a whole execution stack since the object must inherit from the base interface Error
. The interface Error
defines a name, a message, and the stack. The only case where you do not need to throw an instance of an exception is when using the ErrorConstructor
which lets you throw the error directly by using throw Error("Message here that is optional")
.
Get hands-on with 1200+ tech skills courses.