Skip to main content

@throws

Syntax​

@throws {ErrorType} description

Overview​

@throws documents an exception that a function can throw. Use it once for each distinct error type a function may raise. Both the type and description are optional, but providing both makes the contract explicit.

Examples​

Type only​

/**
* @throws {RangeError}
*/
function setAge(age) {
if (age < 0) throw new RangeError('Age cannot be negative.');
this._age = age;
}

Type and description​

/**
* @throws {TypeError} When `name` is not a string.
* @throws {RangeError} When `age` is less than zero.
*/
function createUser(name, age) {}

Without a type​

/**
* @throws Will throw if the database connection fails.
*/
async function saveRecord(record) {}

See also​