The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won’t be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let width = 35.45; //let width = "Hadoooken!"; try { if (isNaN(width)) { throw `Errrooooorrrr...! you entered --> ${width}`; } else { console.log(`you've entered ${width}`); } } catch(e) { console.log(e); } console.log("...moving on to the next line of code..."); |
You can keep throwing the same exception. Once its caught, in your catch block, just do another try/catch so you can throw again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let n = 344; try { throw n; // throws an exception with a numeric value } catch (e) { if (e <= 50) { console.log("statements to handle exceptions 1-50"); } else { try { console.log("cannot handle this exception, so rethrow"); throw e; } catch (e1) { console.log(`2nd catch! ${e1}`); } } } |
Or you can wrap it inside of a try catch. So that whatever is thrown, the outside will catch it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function a() { try { throw n; // throws an exception with a numeric value } catch (e) { if (e <= 50) { console.log("a() - statements to handle exceptions 1-50"); } else { throw e; } } } a() // error |
You’ll get it an error if you run function a, there is nothing to catch it. What we can do is to put function a inside of another try catch like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
let n = 344; function a() { try { throw n; // throws an exception with a numeric value } catch (e) { if (e <= 50) { console.log("a() - statements to handle exceptions 1-50"); } else { throw e; } } } try { a(); // when the second throw happens, we'll be able to catch it out here } catch(e) { console.log(`caught ${e} globally`) } |
output:
caught 344 globally
Custom Errors
we declare a custom error function like so:
1 2 3 4 |
function UserException(message) { this.message = message; this.name = 'UserException'; } |
We will create an instantiation of this function and throw it in another function called getMonthName.
We basically give it a numeric to indicate a month. It uses that number to get the string of the month. Then we
simply return the string.
However, numerically, a month is only 1-12. If we give it say, 15, then in the array, it would be undefined.
In that case, we throw instantiation of our function like so:
1 |
throw new UserException('InvalidMonthNo'); |
As long as the throw is within a try/catch, then it will be valid.
Here is the function that try and catches the block of code. As you can see, once the user puts a erroneous parameter, we can catch it if the array returns undefined.
1 2 3 4 5 6 7 8 9 10 |
function getMonthName(mo) { mo = mo - 1; // Adjust month number for array index (1 = Jan, 12 = Dec) var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; if (months[mo] !== undefined) { return months[mo]; } else { throw new UserException('InvalidMonthNo'); } } |
The reason why our throw will be caught is because our function is wrapped by a try catch.
1 2 3 4 5 6 7 8 |
try { // statements to try var myMonth = 15; // 15 is out of bound to raise the exception var monthName = getMonthName(myMonth); } catch (e) { monthName = 'unknown'; console.log(e.message, e.name); // pass exception object to err handler } |