object oriented programming java n.
Skip this Video
Loading SlideShow in 5 Seconds..
Object-Oriented Programming (Java) PowerPoint Presentation
Download Presentation
Object-Oriented Programming (Java)

Loading in 2 Seconds...

play fullscreen
1 / 44

Object-Oriented Programming (Java) - PowerPoint PPT Presentation

  • Uploaded on

Object-Oriented Programming (Java). 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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

Object-Oriented Programming (Java)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
topics covered today
Topics Covered Today
  • Unit 1.1 Java Applications
    • 1.1.8 Debugging
    • 1.1.9 Debugging with Eclipse
types of errors
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
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
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
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
Strategies to Live By...
  • Debugging is part art, part science.
  • You’ll improve with experience.
strategy 1 debug with purpose
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
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
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
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
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
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
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 debugging1
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 debugging2
The Process of Debugging
  • Identify the code that causes the error.
the process of debugging3
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 debugging4
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
  • 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
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
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
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
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
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 today1
Topics Covered Today
  • Unit 1.1 Java Applications
    • 1.1.8 Debugging
    • 1.1.9 Debugging with Eclipse
code debugging
Code Debugging

public class Debug {

private int something = 0;

private Vector list = new Vector();

public void firstMethod() {


something = something + 1;


public void secondMethod() {


something = something + 2;


public void thirdMethod(int value) {

something = something + value;


public static void main(String[] args) {

Debug debug = new Debug();







Each time we will give some Eclipse tips...

But most of discovery is yours


Main Window


Working Area

monitor the execution
Monitor the Execution
  • On the toolbar, click debug to "launch" the application in debug mode
debug view
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
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
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
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
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
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
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
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
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