Download presentation
Presentation is loading. Please wait.
Published byEustacia Edwards Modified over 9 years ago
1
Object-Oriented Programming (Java)
2
2 Topics Covered Today Unit 1.1 Java Applications –1.1.8 Debugging –1.1.9 Debugging with Eclipse
3
3 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
4
4 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
5
5 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?
6
6 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.
7
7 Strategies to Live By... Debugging is part art, part science. You’ll improve with experience.
8
8 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! }
9
9 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.
10
10 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).
11
11 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.
12
12 Strategy #5:Let others work for you! Sometimes, error detecting tools make certain bugs easy to find. We just have to use them.
13
13 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....)
14
14 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.
15
15 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
16
16 The Process of Debugging Identify the code that causes the error.
17
17 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.
18
18 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.
19
19 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
20
20 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
21
21 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 时暂挂
22
22 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.
23
23 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.
24
24 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.
25
25 Topics Covered Today Unit 1.1 Java Applications –1.1.8 Debugging –1.1.9 Debugging with Eclipse
26
26 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(); }
27
27 Eclipse
28
28 Eclipse Explorer Working Area Main Window
29
29 Tips to Use Eclipse – Show line nubers
30
30 Tips to Use Eclipse – Source Format Source Format
31
31 Debug the Class
32
32 Eclipse - Debug Perspective
33
33 Place a Breakpoint
34
34 Monitor the Execution On the toolbar, click debug to "launch" the application in debug mode
35
35 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
36
36 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
37
37 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
38
38 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
39
39 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
40
40 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
41
41 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
42
42 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
43
43 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
44
44 Diagnose and Fix the Error
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.