Strategies for correcting run-time errors - Rose

Download Report

Transcript Strategies for correcting run-time errors - Rose

Correcting Run-Time Errors
• Small changes and the “What did you just
change?” rule
• Look at the stack
• Use the debugger
• Compare against other code
• Look up Java documentation
These are all recommended practices – not necessarily in order!
Fundamentals of Software
Development 1
Slide 1
Small changes and the “What did you
just change?” rule
• Only make a few changes, then compile and test.
– This hugely simplifies debugging.
– Saves you lots of time in the end.
– Gives you tremendous confidence in your code.
• The first corollary to this rule is this:
– The error is always in the last thing you did.
– Which, if you follow the rule, is easy to spot!
Fundamentals of Software
Development 1
Slide 2
Look at the stack
• Example Java run-time exception dump:
--------------------Configuration: <Default>-------------------This tests the method written, above.
Your input is: asdf
You entered : asdf
 The last visible thing the program did – also a clue!
Exception in thread "main" java.lang.NumberFormatException: For input
string: "asdf"
 Here’s the Java run-time exception dump!
at java.lang.NumberFormatException.forInputString(NumberFormat
Exception.java:48)
 The top line number shown – always a strong clue!
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at LittleTest.main(LittleTest.java:49)
Process completed.
Fundamentals of Software
Development 1
And – What kind of error was it? – In this case,
a “NumberFormatException” – maybe “asdf ” isn’t numeric?
Slide 3
Use the debugger
•
•
We’ll do a demo momentarily.
Key activities:
1.
Make sure “Include debug info” is set.
2.
Likewise – be sure “View… Other Windows… Debug View” is on.
3.
4.
5.
6.
7.
Set breakpoints of interest, to stop the program.
Start the debugger.
Watch the yellow carat – It’s where you are!
Dump selected values by highlighting fields, then “Dump”.
Step or Continue the program.
(can be set at the time of creating a project or later
in Configure… Options…JDK Tools… Default… Parameters
also in Project… Project Properties… JDK Tools)
Fundamentals of Software
Development 1
Slide 4
Compare against other code
• My new Transformer class def starts with:
– public class CountDowner implements StringTransformable
• But all the other Transformer classes start like:
– public class Capitalizer extends StringTransformer
implements StringTransformable
• Maybe that’s why I’m getting this – ?
Exception in thread "AWT-EventQueue-0"
java.lang.ClassCastException: CountDowner
at StringTransformers.add(StringTransformers.java:46)
Fundamentals of Software
Development 1
Slide 5
Look up Java documentation
• What’s that “Integer.parseInt(…” really do?
Class Integer,
method parseInt( )
(back in the Java dump on slide 3)
• Where would you look in the Java Docs?
Checking the documentation is not the last
resort, but a good recommendation to end on!
(Not that a preposition is ever good to end on…)
Fundamentals of Software
Development 1
Slide 6