最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

exception - Do I need to `return` after `throw` in JavaScript? - Stack Overflow

programmeradmin8浏览0评论

I'm throwing an Error from a method of mine that I want an early exit from, as below:

// No route found
if(null === nextRoute) {
    throw new Error('BAD_ROUTE');
}

Do I need to put a return; statement after my throw? It works for me, for now. If it's superfluous I'd rather not put it in, but I can't be sure what different browsers might do.

I'm throwing an Error from a method of mine that I want an early exit from, as below:

// No route found
if(null === nextRoute) {
    throw new Error('BAD_ROUTE');
}

Do I need to put a return; statement after my throw? It works for me, for now. If it's superfluous I'd rather not put it in, but I can't be sure what different browsers might do.

Share Improve this question asked Sep 26, 2014 at 19:44 MattMatt 9,43312 gold badges69 silver badges86 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 249

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.

发布评论

评论列表(0)

  1. 暂无评论