throw error

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.

You can keep throwing the same exception. Once its caught, in your catch block, just do another try/catch so you can throw again.

Or you can wrap it inside of a try catch. So that whatever is thrown, the outside will catch it.

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:

output:

caught 344 globally

Custom Errors

we declare a custom error function like so:

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:

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.

The reason why our throw will be caught is because our function is wrapped by a try catch.