Do uncatchable exceptions exist in Javascript?

后端 未结 2 2236
北海茫月
北海茫月 2021-02-19 23:12

Do any javascript runtimes (browsers, Node, etc.) ever throw uncatchable exceptions? Are any and all exceptions ever encountered in a javascript environment catchable in a try/c

2条回答
  •  不思量自难忘°
    2021-02-19 23:59

    If by exceptions you mean any exceptional condition that breaks your script, then all of them can throw uncatchable exceptions, since most syntax errors aren't catchable. Only syntax errors from dynamically evaluated code (eval, new Function) can be caught.

    try { :( } catch(e) { } // uncatchable syntax error
    

    That's assuming you mean catchable using try..catch. Technically you could use the error event to trap syntax errors from other script blocks:

    
    
    

    On the other hand, maybe you don't consider errors that happen before evaluation to be exceptions. In that case "uncatchable exceptions" may be relegated to exceptions throws from other execution contexts (such as a function invoked with setTimeout), where you don't have control over the execution context throwing the exception. Of course, these exceptions will not disrupt the flow of your program.

提交回复
热议问题