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 the function is synchronous, you can either return some sentinel value that indicates an error and is easily distinguished from an actual result (often null in Javascript) or you can throw an exception or you can return an object that has a property that indicates the success or failure of the operation.
(1) Will any of the code in the method after the throw be executed? YES. If the exception was inside a try then code inside matching catch blocks or finally block will be executed.
No, we can not place any code after throw statement, it leads to compile time error Unreachable Statement.
You do not need to put a return
statement after throw
, the return
line will never be reached as throwing an exception immediately hands control back to the caller.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With