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

javascript - How to get Chrome debugger to break or error when working on undefined variables - Stack Overflow

programmeradmin1浏览0评论

My Javascript code (hundreds of lines) hangs Chrome and when I debug the issue I find out that a variable was undefined. I don't get errors in the console. So this is making my debugging more time consuming because there are no errors or exceptions or anything that tells me where the issue is.

I don't want to add debugging code. Is there a way to make the debugger put out an error, break in the debugger or give an exception or show anything useful for the developer when hitting an undefined variable during runtime? It doesn't have to be for Chrome only.

My Javascript code (hundreds of lines) hangs Chrome and when I debug the issue I find out that a variable was undefined. I don't get errors in the console. So this is making my debugging more time consuming because there are no errors or exceptions or anything that tells me where the issue is.

I don't want to add debugging code. Is there a way to make the debugger put out an error, break in the debugger or give an exception or show anything useful for the developer when hitting an undefined variable during runtime? It doesn't have to be for Chrome only.

Share Improve this question asked Oct 10, 2013 at 21:38 Tony_HenrichTony_Henrich 44.1k80 gold badges252 silver badges388 bronze badges 3
  • The two answers seem to assume that the OP want to explore the stack on exceptions. The way I interpret the question, the issue is that undefined values propagate unnoticed so far that it becomes hard to locate the source of the error, even with scope inspection. Then the question is whether it is possible to configure the browser to report/halt whenever an undefined is used as a key or in a numerical or Boolean calculation, instead of the default behavior of undefined, NaN, false, respectively (sigh!). If this is the intended question, maybe it needs to be clarified to attract better answers. – Elias Hasle Commented May 7, 2018 at 11:55
  • @Elias I don't know if I need to change the default behavior or if it can be done. I don't know what I don't know. Maybe you can add an answer which can be helpful to me and others. This question is almost 5 years old! I don't even remember what the issue was and of course I am past it by now. I am even working at a different company. – Tony_Henrich Commented May 8, 2018 at 17:31
  • Hi @Tony. :-) I see. I am afraid I can't help. I found the question while in search of the answer... – Elias Hasle Commented May 8, 2018 at 19:05
Add a comment  | 

2 Answers 2

Reset to default 11

You can break into the DevTools debugger when a JavaScript error occurs using the Pause on JavaScript Exceptions feature. It has two active modes; pause on all exceptions, and pause on uncaught exceptions.

Based on the description of your experience, the application you are working on may have errors that are caught but not re-thrown or logged. Using the "Pause on All Exceptions" (blue colored pause icon), will help in this scenario.

Note: some libraries, like jQuery, catch exceptions and do not re-throw them. If you have this experience, you may need to advance past these exceptions or set the "Pause on All Exceptions" feature after all dependencies have loaded.

window.onerror = function() { debugger; }
发布评论

评论列表(0)

  1. 暂无评论