Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to `return` after `throw` in JavaScript?

People also ask

Does function stop after throw?

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.

What is difference between return and throw?

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.

Does code after throw get executed?

(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.

Can we write code after throw?

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.