Slides - School of Computer Science

Download Report

Transcript Slides - School of Computer Science

School of Computer Science &
Information Technology
G6DICP - Lecture 6
Errors, bugs and debugging
Types of Errors

Syntax errors



Semantic errors


2
Mistakes in the rules of the language itself
Illegal Java - errors of grammar or punctuation
Mistakes in the logic of the code
Legal Java - but not what you intend!
Where errors show themselves

Compile-time errors



Run-time errors



Some syntax errors are detected by the VM when the
program is run
Legal Java - that causes problems under certain
conditions.
Logical errors

3
Many syntax errors are detected by the compiler
The compiler will generate an error message including a line number
Unexpected results - bugs!
Compiler Errors

If javac gives no output, the compilation is successful
C:\code javac fname.java
error: cannot read: fname.java
1 error
C:\code

Cannot read… compiler error




4
This means that the source file (fname.java) has not been read.
Usually a typo in the file name or javac command
Remember Java is case sensitive!
Could be file privileges
Compiler Errors (2)


Most compiler errors have a file name and line number
This tells you where the error was detected
C:\code javac fname.java
fname.java:23: cannot resolve symbol
symbol : class string
location: class fname
string msg;
^
1 error
5
Compiler Errors (cont.)


Most compiler errors have a file name and line number
This tells you where the error was detected
File name (fname.java)
C:\code javac fname.java
fname.java:23: cannot resolve symbol
symbol : class string
location: class fname
string msg;
^
1 error
6
Compiler Errors (cont.)


Most compiler errors have a file name and line number
This tells you where the error was detected
Line number (23)
C:\code javac fname.java
fname.java:23: cannot resolve symbol
symbol : class string
location: class fname
string msg;
^
1 error
7
Compiler Errors (cont.)


Most compiler errors have a file name and line number
This tells you where the error was detected
Type of error
C:\code javac fname.java
fname.java:23: cannot resolve symbol
symbol : class string
location: class fname
string msg;
^
1 error
8
Compiler Errors (cont.)


Most compiler errors have a file name and line number
This tells you where the error was detected
Details of error
Type of error
C:\code javac fname.java
fname.java:23: cannot resolve symbol
symbol : class string
location: class fname
string msg;
^
1 error
9
Compiler Errors (cont.)


Most compiler errors have a file name and line number
This tells you where the error was detected
Location of error
C:\code javac fname.java
fname.java:23: cannot resolve symbol
symbol : class string
location: class fname
string msg;
^
1 error
10
Compiler Errors (3)

The point at which the error is detected is not
always the point at which there is a mistake in
the code
 For example unbalanced braces cannot be
detected until the braces are closed
 Compiler errors can have knock-on effects, with
other “spurious” errors being caused by the first

11
ADVICE - fix the first compiler error, and attempt to
compile before looking at others - they might
disappear!
Runtime Errors


Runtime errors occur while the program is running,
although the compilation is successful
Usually runtime errors consist of “exceptions” in a thread

Errors in the main method, generate exceptions in thread “main”
Exception in thread "main" java.lang.NoClassDefFoundError: fname

NoClassDefFoundError



12
Caused if the interpreter can’t find the named class file
(e.g. fname.class)
This is usually a typo - either in the command line, the class
declaration or the file name
Remember Java is case sensitive!
Causes of Runtime Errors


Errors that only become apparent during the course of
execution of the program
External Factors - e.g.





Internal Factors - e.g.




13
Out of memory
Hard disk full
Insufficient i/o privileges
etc.

Arithmetic errors
Attempts to read beyond the end of a file
Attempt to open a non-existent file
Attempts to read beyond the end of an array
etc.
Exceptions

When a potential error condition occurs while a
program is running an Exception occurs


Exceptions are of specific types - e.g.




14
For example attempting to read from a non-existent
file
ArithmeticException
IOException
ArrayIndexOutOfBoundsException
If the program contains code to handle the
exception that code is triggered.
 If there is no code to handle the exception then
the program terminates with a runtime error
Missing Class File

This can be either a compiler error or a runtime
error!
 If a class file used by the program is missing at
compile time, then a compiler error is generated
 If the program is successfully compiled, but a
class file is missing when it is run, then a runtime error is generated

15
NoClassDefFoundError
Null Pointer Exceptions

These are generated if your program atempts to
access something non-existent in memory
 Internally these are drastic, but they can be
triggered by extremely subtle errors
 They do not always provide a line number (or
that line might not be where the error is)
 They can be extremely hard to debug
16
Logical Errors
The program compiles and runs, but it doesn’t do
what is intended
 May be caused by:




These are “bugs”

17
Some syntax errors (i.e. legal Java that
is wrong in the current context)
Errors in logical design
Term coined by Admiral Grace
Hopper for anything that causes
a program to do something
unexpected
Debugging

Paper helps - examine your flow charts
 Code tools




Flags - code that tells you where you are in a program
Breaks - code that stops the program
Watches - code that prints the contents of variables
Debuggers

Software that implements the above
 Also runs code visually in “slow motion”
 Built into most integrated development environments
 Invaluable for debugging large, complex programs
18