Java Reference
In-Depth Information
The finally part is a good place to put any cleanup code that needs to be executed regardless of any
errors that occurred previously.
You've seen the top mistakes made by developers, and you've also seen how to handle errors in your
code. Unfortunately, errors will still occur in your code, so let's take a look at one way to make remedy-
ing them easier by using a debugger.
Debugging
JavaScript is traditionally looked upon as a diffi cult language to write and debug due to the lack of decent
development tools. This is not the case now, however, thanks to many tools made available to developers.
Most notably are the debugging tools available for Internet Explorer, Firefox, Safari, and Opera. With
these tools, you can halt the execution of your script with breakpoints and then step through code line
by line to see exactly what is happening.
You can also fi nd out what data is being held in variables and execute statements on the fl y. Wit hout
debuggers, the best you can do is use the alert() method in your code to show the state of variables
at various points.
Debugging is generally universal across all browsers, and even languages. Some debugging tools
may offer more features than others, but for the most part, the following concepts can be applied to
any debugger:
Breakpoints tell the debugger it should break, or pause code execution, at a certain point. You
can set a breakpoint anywhere in your JavaScript code, and the debugger will halt code execu-
tion when it reaches the breakpoint.
Watches allow you to specify variables that you want to inspect when your code pauses at a
breakpoint.
The call stack is a record of what functions and methods have been executed to the breakpoint.
The console allows you to execute JavaScript commands in the context of the page and within
the scope of the breakpoint. In addition, it catalogs all JavaScript errors found in the page.
Stepping is the most common procedure in debugging. It allows you to execute one line of code
at a time. There are three ways to step through code.
Step Into executes the next line of code. If that line is a function call, the debugger exe-
cutes the function and halts at the fi rst line of the function.
Step Over, like Step Into, executes the next line of code. If that line is a function, Step
Over executes the entire function and halts at the fi rst line outside the function.
Step Out returns to the calling function when you are inside a called function. Step Out
resumes the execution of code until the function returns. It then breaks at the return
point of the function.
Before delving into the various debuggers, let's create a page you can debug. Note the deliberate typo in
line 16. Be sure to include this typo if creating the page from scratch.
Search WWH ::




Custom Search