January 2020
Intermediate to advanced
548 pages
13h 36m
English
Throwing is a shift of control from the current statement to the nearest containing try...catch statement on the call stack. If no such try...catch statement exists, then the execution of the program will terminate entirely. Throwing is conventionally used to raise exceptions when specific requirements or expectations are not met:
function nameToUpperCase(name) { if (typeof name !== 'string') { throw new TypeError('Name should be a string'); } return name.toUpperCase();}
To catch this error, we would need to have a try...catch block somewhere on the call-stack, wrapping the call to the nameToUpperCase function or the call to the function that calls it (and so on):
let theUpperCaseName;try { theUpperCaseName = nameToUpperCase(null); ...Read now
Unlock full access