In this article, I will explain a simple, but very useful technique of debugging using Breakpoints.
The example code above simple sets credentials for a SMTP server and sends an email.
Now, set a "breakpoint" by clicking the grey bar on the left of the line of code.
When a line of code is set as a breakpoint, the program execution will pause every time that line is getting invoked. This lets you stop the program at the desired point and examine the intermediate state of the program. The background colour of the line changes to indicate the breakpoint. Now, Debug the code by pressing F5.
The program execution stops at breakpoint. The current program execution point is highlighted in yellow, and some information is displayed at the bottom panel.
Locals panel shows all local variables that exists (even if null) and you can look into object instances for the values they contain, too. If the breakpoint is within an object instance, "this" object contains all its fields.
Call Stack panel shows the method calls that led to the current breakpoint. In other words, you can go down the call stack to see what method call steps were involved in eventually invoking the current breakpoint.
When you are done exploring the state at the breakpoint, you can continue executing the program by pressing F5. If there are any other breakpoints after the current one, it will automatically jump to that breakpoint and pause (this can also happen when you set only one breakpoint, in the case where the line of code you set as a breakpoint is a part of a loop, therefore is getting invoked multiple times).
Clicking the red circle on the breakpoint will deselect the line. Alternatively, you can press Ctrl+Shift+F9 to remove all breakpoints.
To stop debugging and exit the program, press Shift + F5.
Continue reading about more advanced usage of breakpoints
No comments:
Post a Comment