Throw statements are used in combination with Try-Catch staements to define an user defined exceptions. When an error is caught during execution of a try block, the throw statement is executed and program flow goes to catch statements as needed.
NOTE: The code after the throw statement is not executed. If there is no catch block defined, the program terminates immediately.
Syntax for Throw statement
throw expression;
Where:
- expression – It is expression which gets thrown when error occurs.
While throwing an exception an object can be referenced too. That can be a JavaScript error object such as RangeError, EvalError, SyntaxError … etc or an user defined object. A throw exception can also be re-thrown, until an exception handler is found or more debugging information is found.
Example of user-defined error object throwing error message
Example with multiple throw statements and JavaScript error objects
Comments
No comments have been made yet.
Please login to leave a comment. Login now