如何捕获“超出最大调用堆栈大小”错误? [英] how to catch a "Maximum call stack size exceeded" error?

查看:239
本文介绍了如何捕获“超出最大调用堆栈大小”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  try {
     requestAnimationFrame(function re(){
       re();
     })}
  catch (e){
       console.log(e);
     }

我在控制台上尝试了上面的代码,它没有按预期工作。在我看来,虽然

I tried above code in console, it doesn't work as expected. It seems to me that although

 requestAnimationFrame(function re(){
       re();
     })}

最终会触发错误,什么引发尝试反而是动画的id。如何捕获这样的超出最大调用堆栈大小错误?

will eventually trigger an error, what throws to the try is insteadly the animation's id. How do I catch such a "Maximum call stack size exceeded" error?

推荐答案

关于捕获最大堆栈大小的事情超出错误我不确定它会如何起作用。浏览器唯一能够捕获错误的东西是空的堆栈然后让下一个函数运行是catch函数但是那时(我不认为浏览器甚至可以得到它那么远)你会没有迹象表明你的代码在哪里破了,所以你试图运行的任何东西最多都会有严重错误。

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.

另一个问题(可能是没有浏览器有这个的原因)是因为在错误之后运行的大多数函数都会尝试以某种方式重新启动正常的Web函数。因为你不知道什么破坏了你不能阻止它再次发生所以你最终会一次又一次地崩溃它。这将使用户和浏览器都对您的网页感到愤怒。

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

这篇关于如何捕获“超出最大调用堆栈大小”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆