how to catch a “Maximum call stack size exceeded” error?

前端 未结 4 1235
走了就别回头了
走了就别回头了 2021-01-14 17:58
  try {
     requestAnimationFrame(function re(){
       re();
     })}
  catch (e){
       console.log(e);
     }

I tried above code in console, i

4条回答
  •  别那么骄傲
    2021-01-14 18:24

    the thing about catching a maximum stack size exceeded error is I'm not sure how it would work. The only thing that the browser could do to allow the error to be caught is empty the stack then have the next function run be the catch function but at that point (and I don't think browsers can even get it that far) you would have no indication on where your code broke so anything you tried to run would be severely buggy at best.

    Another problem (which is probably why no browsers have this) is because most functions you would run after the error would simply try to restart normal web function in some way. Since you don't know what broke you can't stop it from happening again so you would end up crashing it again and again. Which would make both the users and the browsers mad at your web page.

    So I guess the best that can be done is make sure your code doesn't have infinity loops and is able to run smoothly

    a good description of stack size and how it works can be found in the answer to this question

提交回复
热议问题