Forget about System.out.println()! Try Eclipse
Debugger~~
A debugger is a set of tools that allows us to suspend a
running program and look at the contents of variables inside the program.
With debugger, you can
- Debug our code
- Get familiar with a new program
- Get a better understanding of JAVA and OOP
You can debug if you can
- Learn how to step through a program
- Learn how to examine the contents of program variables
Start a debug session, you need to
- Tell debugger where to pause
- Run the program in debug mode
Primary
Debug Skills
Stack
Frame
Class
-> Thread
-> Stack Frame
-> Thread
-> Stack Frame
Each stack frame provides a context for variables. The
top frame always points to where we are in the program right now.
Buttons
Step Into: to the method being called – F5
Step Over: execute current line and stop at next line – F6
Step Return: going back to the previous level – F7
Step Filters: When you want to debug your own code and skip Java language classes and third-party classes.
-> right click debug window -> edit step filters -> click use step filters
Resume: continue running the program until it completes or the next breakpoint (Run to Line)
Step Return: going back to the previous level – F7
Step Filters: When you want to debug your own code and skip Java language classes and third-party classes.
-> right click debug window -> edit step filters -> click use step filters
Resume: continue running the program until it completes or the next breakpoint (Run to Line)
Drop to Frame: go “back in time” where we were
Expression
Window -> show view -> expressions -> add new
expression
Use Inspect command to look at any expression and see
what its current value is. The limitation is that you can’t use code assist or
content assist when you type in the expression. One work around is window ->
show view -> display, here you can take type expression with code assist and
content assist. When you high light the expression in display and right click
then say inspect, then Ctrl + Shift + I to move to expression view. However, it is just a snapshot, but not an
actual dynamic watch expression at this point. We can convert it to watch
expression easily. In display view, you can execute the code as you could do in
the Eclipse scrapbook.
Tips
Select Enclosing Element – ALT + SHIFT + UP (then Ctrl + Shift + I to inspect or Ctrl + Shift + D to display)
Watch Expression - ALT + SHIFT + UP then watch
Exception
Breakpoints
We don’t need to know which lines cause the exception, we
just tell the debugger to pause wherever it finds one.
Caught and uncaught exception, when running JUnit tests,
all exceptions are caught. So if we need to use exception breakpoint with
JUnit, make sure we always tick caught exception. If we enable suspend
execution on uncaught exceptions in debug preferences, it can catch uncaught
exception in Java application debug mode, but not in JUnit debug mode.
Hot
Code Replace
When we save our hot code change, the debug pointer will
go back to the previous line. Press resume checking your hot fix.
Extra
Debug Skills
Conditional
Breakpoint
In breakpoint property, you can find Hit Count, which
means don’t suspend until this breakpoint has been hit this number of times.
Enable condition, like this.getBook().size() > 0.
Watch
Points
For field variables, default suspends on field access and
field modification.
Class
Prepare Breakpoint
Only stop the first time we load the new class.
Stop
in Main
Run -> Open debug dialog -> Java application ->
Class -> Main -> Stop in main
Suspend
Pause anytime when you debug a long program.
Step
Into Selection
Ctrl + Alt then hover over the method name. In many
real-life programs, we may have more than two separate methods in one line, you
can step into the exactly method you want with step-into-selection. (Ctlr +
Click to open source declaration, you must have already known this.)
Debug
Java System Classes
By default, Eclipse uses the normal JRE, so we need to
configure Eclipse to use the special JRE. Window -> Preferences -> Java ->
Install JREs ->
You may need to cancel Step Filters.
You may need to cancel Step Filters.
Reference
No comments:
Post a Comment