500 likes | 668 Views
Learn about types of errors, debugging strategies, and the process of debugging in Java programming. Find out how to use debugging tools effectively and make your coding process more efficient.
E N D
Topics Covered Today • Unit 1.1 Java Applications • 1.1.8 Debugging • 1.1.9 Debugging with Eclipse
Types of Errors • There are three general types of errors: • Syntax (or “compile time”) errors • Syntax errors are “grammatical” errors and are detected when you compile the program • Syntax errors prevent your program from executing • Runtime errors • Runtime errors occur when you tell the computer to do something illegal • Runtime errors may halt execution of your program • Logic errors • Logic errors are not detected by the computer • Logic errors cause your results to be wrong
What to Do about Errors? • Error messages are your friends • read them and try to understand them • With practice, you can fix most syntax errors almost immediately • Runtime and logic errors may take considerably longer to track down and fix • Here’s what’s important to remember: • Everyone makes lots of stupid errors (and almost all errors are stupid ones--mine included); it’s nothing to be ashamed of • However, it is not OK to let those errors survive • Approximately 90% of your time will be spent debugging
What is Debugging? • You tell me! Everybody writes codes with bugs. • Things to think about: • What caused the bug? • How did you end up finding it? • How could you have avoided the bug in the first-place?
Debugging Philosophy • Think about why you believe the program should produce the output you expected. • Make assertions until you understand how your view differs from the computer.
Strategies to Live By... • Debugging is part art, part science. • You’ll improve with experience.
Strategy #1:Debug with Purpose • Don' t just change code and hope you' ll fix the problem! • Instead, make the bug reproducible, then use methodical hypothesis testing? • While(bug) { • Ask, what is the simplest input that produces the bug? • Identify assumptions that you made about program operation • that could be false. • Ask yourself how does the outcome of this test/change guide • me toward finding the problem? • Use pen & paper to stay organized! • }
Strategy #2: Explain it to Someone Else • Often explaining the bug to someone unfamiliar with the program forces you to look at the problem in a different way.
Strategy #3: Focus on Recent Changes • If you find a NEW bug, ask: • what code did I change recently? • This favors: • writing and testing code incrementally • regression testing (making sure new changes don' t break old code).
Strategy #4 : Get Some Distance... • Sometimes, you can be TOO CLOSE to the code to see the problem. • Go for a run, take a shower, whatever relaxes you but let' s your mind continue to spin in the background.
Strategy #5:Let others work for you! • Sometimes, error detecting tools make certain bugs easy to find. We just have to use them.
Strategy #6:Think Ahead • Bugs often represent your misunderstanding of a software interface. • Once you' ve fixed a bug: • Smile and do a little victory dance.... • Think about if the bug you fixed might manifest itself elsewhere in your code • Think about how to avoid this bug in the future • (maybe coding 36 straight hours before the deadline isn' t the most efficient approach....)
The Process of Debugging • To save testing time, create a small test case that reproduces the error. • Implement the test case in a separate class or in the method main of the program being debugged.
The Process of Debugging • A good place to start the search is the test case itself: find the line of code in the test case where the error appears
The Process of Debugging • Identify the code that causes the error.
The Process of Debugging • Don't assume that the error is caused by bad logic in this code. It might be caused by bad data that originated in another area of the code. • If this is true, then locate the code where the bad data originated and repeat from step 2.
The Process of Debugging • The fix should not introduce new errors. The developer should understand how the fix affects other parts of the code because a fix can have unexpected side effects.
Print Statements • Print statements are used to display important information as the code executes. • This information may include: • The called methods • The value of parameters • The value of loop control variables • The value of local variables • The value of instance variables
The Debugger • A debugger is a convenient tool for locating the source of errors. • Types of Java debuggers: • Integrated development environments (IDEs), such as Eclipse,NetBean IDE, Borland JBuilder, and BlueJ, contain their own debuggers. • Stand-alone graphical debuggers, such as JSwat • Text-based debuggers, such as Sun JDB
Capabilities of Debugger - 1 • Setting breakpoints. • A breakpoint is a marker in the code that indicates to the debugger to stop the execution of the program before executing a certain statement. • Breakpoint Types • Line 此语句执行前暂挂 • Method 进入/离开此方法时暂挂 • Field (Watchpoint) 监视点,成员变量被读取或修改时暂挂 • Exception 捕获到Exception时暂挂
Capabilities of Debugger - 2 • Stepping through a program. • resume. Continue with the execution, not single stepping. • step into. Execute the current line. If current line calls a method, the debugger "steps into" the method, that is, execution moves to the called method and stops before the first line in the called method. • step over. Execute the current line. If current line calls a method, the debugger "steps over" the method, that is, the method is executed and execution stops before the next line in the current method. • step return. Execute until current method completes and return to the calling method. Execution stops before the next line in the calling method.
Capabilities of Debugger - 3 • Examine the data. • The debugger can display the values of the variables in the current method and class. • If a variable is a compound object that contains multiple elements, the user can "open" the object and inspect the value of each element.
Capabilities of Debugger - 4 • Stack trace. • When a breakpoint is hit and execution is suspended, the debugger can display the sequence of called methods. • The user can examine the values of local variables in the called methods.
Topics Covered Today • Unit 1.1 Java Applications • 1.1.8 Debugging • 1.1.9 Debugging with Eclipse
Code Debugging public class Debug { private int something = 0; private Vector list = new Vector(); public void firstMethod() { thirdMethod(something); something = something + 1; } public void secondMethod() { thirdMethod(something); something = something + 2; } public void thirdMethod(int value) { something = something + value; } public static void main(String[] args) { Debug debug = new Debug(); debug.firstMethod(); debug.secondMethod(); } }
Eclipse Eclipse Each time we will give some Eclipse tips... But most of discovery is yours
Eclipse Main Window Explorer Working Area
Tips to Use Eclipse – Source Format Source Format
Monitor the Execution • On the toolbar, click debug to "launch" the application in debug mode
Debug View • Shows: • Active threads • Current stack frame when execution has stopped • Previous stack frames • Method and variables are shown in the editor for the selected frame • Update in the editor updates the source
Variables View • Shows all fields of instance where breakpoint occurred • Select this to see all fields • Select any field to see value • If field is bound to an object, you can select Inspect from the menu to view its fields and values
Changing Field Values • To change field value: • Select field in Variables view • Select Change Value… from the context menu • Enter new value into Set Variable Value window • Click OK
Expressions View • Remembers all objects you have inspected • Displays the fields of the object • You can see the values of the fields • You can Inspect the fields • Opens when: • You Inspect an object • You click on the Watch from the context menu
Breakpoint View • Lists all available breakpoints • Can be used for manipulating breakpoints (through the views menu): • Enabling • Disabling • Removing • Also displays breakpoints properties • Accessed like other debugging views
Method Breakpoints • To set method breakpoint: • Select method in the Outline View • From context menu select Toggle Method Breakpoint • To set breakpoint’s properties: • Select breakpoint in editor • Select Breakpoint Properties.. from context menu • Set properties as desired • Entry, exit, enable hit count • Execution suspends on entry/exit into method
Field Breakpoints • Also known as watchpoint • To set the watchpoint: • Select field in the Outline View • From context menu select Toggle Watchpoint • To set watchpoint’s properties: • Select breakpoint in editor • Select Breakpoint Properties.. from context menu • Set properties as desired • Access/modification, enable • Execution suspended on access/modification of field
Java Exception Breakpoint • To Add Java Exception Point: • Select Add Java Exception Point from menu • Enter exception type • Specify what triggers a breakpoint: • Caught exception • Uncaught exception • Both
How To Debug • Here are simple steps for debugging in Eclipse: • Set your breakpoints • Hit a breakpoint during execution • Walk/step through code to other breakpoints • Follow along in editor • Inspect/Watch interesting fields • Watch the Console for things to happen