Throwing custom errors
In JavaScript programming, programmers can create and throw custom errors in a program according to their software development needs for error handling in a program. By applying custom errors in a JavaScript program, programmers can create custom errors in the program and add source references to multiple programs. Such as, creating custom error messages, error codes in the program, or displaying new properties, etc., which makes it easier for the programmer to debug errors in a program and control and manage program error situations in the proper order.

Syntax of Error Throw in JavaScript.
The throw statement in a JavaScript program is used to throw custom exceptions in a program. JavaScript programmers can create built-in custom errors like Error, TypeError, or RangeError and throw them in the program, apart from this, programmers can create custom error classes as per the need.
throw new Error(“display custom throw statement”);
Creating a custom error in JavaScript.
To create custom errors in JavaScript programming, programmers can apply and extend the built-in Error class. Where JavaScript programmers can create individual error classes, they can apply new properties or methods to the custom error. Which provides detailed information about the error in a program.
A new custom error class example in JavaScript.
class manualError extends Error {
constructor(errormessage, error_Code) {
super(errormessage); // here it Call the parent class constructor object
this.error_name = this.constructor.error_name; // we can Set the name of the error to the class name
this.error_Code = error_Code; // let we Add a custom error code property here
}
}
throw new manualError(“invalid value input”, 200);
Explanation of Creating a custom error.
Here in the current program, manualError extends Error custom class.
errormessage and error_Code pass constructor values.
Here super(message) call method will invoke parent class (Error) constructor to initialize the error message.
this.error_name = this.constructor.error_name defines the name of the error to the name of the class, here in this condition it is manualError.
Where error_Code is a custom property method in the program which can be used by programmers to define a special error type.
Use of custom error class in JavaScript program.
After JavaScript programmers define a custom error class as per their application development requirement, they can throw any other type of error message in it. Where any custom error can be caught in the catch block method and can be controlled and managed.
Example of throw and catch block method to throw custom error in JavaScript program.
class manualError extends Error {
constructor(errormessage, error_Code) {
super(errormessage);
this.errorname = this.constructor.errorname;
this.error_Code = error_Code;
}
}
function testvalue(test) {
if (!test) {
throw new manualError(“Enter test data”, 200); // it used to Throw custom error if testvalue is missing
}
console.log(“enter Data done successfully”);
}
try {
testvalue(null); // here it used to throw a CustomError
} catch (error) {
if (error instanceof manualError) {
console.log(“it caught custom error -“, error.message);
console.log(“Display Error Code -“, error.error_Code);
} else {
console.log(“May be unexpected error generated -“, error);
}
}
Explanation of custom error class.
Here in this program if the data is not entered then the testvalue() function throws a manualError.
Here the catch block method checks if the error in the current program is an instance of manualError, it displays a custom message and error code in the active program.
Handling custom errors with additional properties in JavaScript.
Individual custom errors in JavaScript programs can have any other properties added to them, such as a timestamp, state, or any other information that helps the programmer understand or manage errors in the current program in the proper order.
Example of additional properties in JavaScript programming.
class dbError extends Error {
constructor(errormessage, query, errorCode) {
super(errormessage);
this.dbname = this.constructor.dbname;
this.query = query; // it used to Store the database query which caused the error
this.errorCode = errorCode; // let create here Custom error code
this.timestamp = new Date(); //let Add a timestamp of when the error occurred in database
}
}
function launchQuery(query) {
if (query === “”) {
throw new dbError(“Query should not be empty”, query, 200);
}
console.log(“required Query run successfully”);
}
try {
launchQuery(“”); // let’s create a multiple tray and catch method that will throw a DatabaseError
} catch (error) {
if (error instanceof dbError) {
console.log(“Display Database Error -“);
console.log(“Display error message -“, error.message);
console.log(“Display database Query -“, error.query);
console.log(“Display Error Code -“, error.errorCode);
console.log(“Display Timestamp -“, error.timestamp);
} else {
console.log(“Produce any Unexpected error -“, error);
}
}
Explanation of additional properties in JavaScript.
The dbError class in this program is extending a Database Error class, and adds properties such as errorCode, and timestamp for the database query.
Here in the launchQuery() function, if the query is an empty string, a DatabaseError will be displayed.
Here the catch block method manages and handles the DatabaseError in the current program, and displays the custom properties.
Summary of error handling and custom error throwing.
- Throwing errors in a program – JavaScript programmers can throw custom errors by applying the throw keyword in a JavaScript program, where these custom errors can be instances of the built-in Error class or custom classes that extend Error.
- Creating a custom error class – Here programmers can create a custom individual error class, such as manualError, and add custom properties such as error code, timestamp, or context-specific description.
- Catching custom errors in the program – Here programmers can catch various custom errors in the existing program, and handle them in the catch block method by applying instanceof method to test the error type.
- Best practice of custom errors and throws keyword – Here extend the Error class, use super(message) parent method, and add additional class properties to make your custom errors more helpful and easy to handle.