In JavaScript try...catch ... Statement, we learned how to catch a system-thrown error. JavaScript allows us to customize the error and throw the error message.
Throw statement
The throw statement allows us to create custom errors.
The correct technical term is: Create or throw an exception (exception).
If you use throw with try and catch, you can control the flow and generate custom error messages.
Throw statement syntax
Throw exception
An exception can be a JavaScript string, a number, a logical value, or an object.
In the following example, we define a division calculation function that throws an exception if the dividend of the function is 0. At the time of the call with Try...catch ... To handle:
<! DOCTYPE html>//Trigger Operation functiondivsion (NUM1, num2) {if(num2 = = 0) { Throw"Dividend cannot be 0"; } returnNUM1/num2; } functionBtn_click () {varNUM1 = number (document.getElementById ("TxtNum1"). Value); varnum2 = number (document.getElementById ("txtNum2"). Value); Try { varresult =divsion (NUM1, num2); document.getElementById ("Txtresult"). Value =result; } Catch(ERR) {alert (ERR); } } </script>JavaScript Throw statement