April 2018
Beginner
536 pages
13h 21m
English
When a runtime error takes place, an instance of the Error class is thrown:
throw new Error();
We can create custom errors in a couple of different ways. The easiest way to achieve it is by passing a string as an argument to the Error class constructor:
throw new Error("My basic custom error");
If we need more customizable and advanced control over custom exceptions, we can use inheritance to achieve it:
export class Exception extends Error {
public constructor(public message: string) {
super(message);
// Set the prototype explicitly.
Object.setPrototypeOf(this, Exception.prototype);
}
public sayHello() {
return `hello ${this.message}`;
}
}
In the preceding code snippet, we have declared a class named Exception, which inherits ...