18
chool of Computer Science & Information Technology G6DICP - Lecture 6 G6DICP - Lecture 6 Errors, bugs and debugging Errors, bugs and debugging

School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

Embed Size (px)

Citation preview

Page 1: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

School of Computer Science & Information Technology

School of Computer Science & Information Technology

G6DICP - Lecture 6G6DICP - Lecture 6

Errors, bugs and debuggingErrors, bugs and debugging

Page 2: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

2

Types of ErrorsTypes of Errors

Syntax errorsSyntax errors Mistakes in the rules of the language itselfMistakes in the rules of the language itself Illegal Java - errors of grammar or punctuationIllegal Java - errors of grammar or punctuation

Semantic errorsSemantic errors Mistakes in the logic of the codeMistakes in the logic of the code Legal Java - but not what you intend!Legal Java - but not what you intend!

Page 3: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

3

Where errors show themselvesWhere errors show themselves Compile-time errorsCompile-time errors

Many syntax errors are detected by the compilerMany syntax errors are detected by the compiler The compiler will generate an error message - The compiler will generate an error message -

including a line numberincluding a line number

Run-time errorsRun-time errors Some syntax errors are detected by the VM when the Some syntax errors are detected by the VM when the

program is runprogram is run Legal Java - that causes problems under certain Legal Java - that causes problems under certain

conditions.conditions.

Logical errorsLogical errors Unexpected results - bugs!Unexpected results - bugs!

Page 4: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

4

Compiler ErrorsCompiler Errors If javac gives no output, the compilation is successfulIf javac gives no output, the compilation is successful

Cannot read… compiler errorCannot read… compiler error This means that the source file (fname.java) has not been read.This means that the source file (fname.java) has not been read. Usually a typo in the file name or javac commandUsually a typo in the file name or javac command Remember Java is case sensitive!Remember Java is case sensitive! Could be file privilegesCould be file privileges

C:\code javac fname.javaerror: cannot read: fname.java1 errorC:\code

Page 5: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

5

Compiler Errors (2)Compiler Errors (2) Most compiler errors have a file name and line numberMost compiler errors have a file name and line number This tells you where the error was detected This tells you where the error was detected

C:\code javac fname.javafname.java:23: cannot resolve symbolsymbol : class string location: class fname string msg; ^1 error

Page 6: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

6

Compiler Errors (cont.)Compiler Errors (cont.) Most compiler errors have a file name and line numberMost compiler errors have a file name and line number This tells you where the error was detected This tells you where the error was detected

C:\code javac fname.javafname.java:23: cannot resolve symbolsymbol : class string location: class fname string msg; ^1 error

File name (fname.java)File name (fname.java)

Page 7: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

7

Compiler Errors (cont.)Compiler Errors (cont.) Most compiler errors have a file name and line numberMost compiler errors have a file name and line number This tells you where the error was detected This tells you where the error was detected

C:\code javac fname.javafname.java:23: cannot resolve symbolsymbol : class string location: class fname string msg; ^1 error

Line number (23)Line number (23)

Page 8: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

8

Compiler Errors (cont.)Compiler Errors (cont.) Most compiler errors have a file name and line numberMost compiler errors have a file name and line number This tells you where the error was detected This tells you where the error was detected

C:\code javac fname.javafname.java:23: cannot resolve symbolsymbol : class string location: class fname string msg; ^1 error

Type of errorType of error

Page 9: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

9

Compiler Errors (cont.)Compiler Errors (cont.) Most compiler errors have a file name and line numberMost compiler errors have a file name and line number This tells you where the error was detected This tells you where the error was detected

C:\code javac fname.javafname.java:23: cannot resolve symbolsymbol : class string location: class fname string msg; ^1 error

Type of errorType of errorDetails of errorDetails of error

Page 10: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

10

Compiler Errors (cont.)Compiler Errors (cont.)

Most compiler errors have a file name and line numberMost compiler errors have a file name and line number This tells you where the error was detected This tells you where the error was detected

C:\code javac fname.javafname.java:23: cannot resolve symbolsymbol : class string location: class fname string msg; ^1 error

Location of errorLocation of error

Page 11: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

11

Compiler Errors (3)Compiler Errors (3)

The point at which the error is detected is not The point at which the error is detected is not always the point at which there is a mistake in always the point at which there is a mistake in the codethe code

For example unbalanced braces cannot be For example unbalanced braces cannot be detected until the braces are closeddetected until the braces are closed

Compiler errors can have knock-on effects, with Compiler errors can have knock-on effects, with other “spurious” errors being caused by the firstother “spurious” errors being caused by the first ADVICE - fix the first compiler error, and attempt to ADVICE - fix the first compiler error, and attempt to

compile before looking at others - they might compile before looking at others - they might disappear!disappear!

Page 12: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

12

Runtime ErrorsRuntime Errors Runtime errors occur while the program is running, Runtime errors occur while the program is running,

although the compilation is successfulalthough the compilation is successful Usually runtime errors consist of “exceptions” in a threadUsually runtime errors consist of “exceptions” in a thread

Errors in the main method, generate exceptions in thread “main”Errors in the main method, generate exceptions in thread “main”

NoClassDefFoundErrorNoClassDefFoundError Caused if the interpreter can’t find the named class file Caused if the interpreter can’t find the named class file

(e.g. fname.class)(e.g. fname.class) This is usually a typo - either in the command line, the class This is usually a typo - either in the command line, the class

declaration or the file namedeclaration or the file name Remember Java is case sensitive!Remember Java is case sensitive!

Exception in thread "main" java.lang.NoClassDefFoundError: fname

Page 13: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

13

Causes of Runtime ErrorsCauses of Runtime Errors Errors that only become apparent during the course of Errors that only become apparent during the course of

execution of the programexecution of the program External Factors - e.g.External Factors - e.g.

Out of memoryOut of memory Hard disk fullHard disk full Insufficient i/o privilegesInsufficient i/o privileges etc.etc.

Internal Factors - e.g.Internal Factors - e.g. Arithmetic errorsArithmetic errors Attempts to read beyond the end of a fileAttempts to read beyond the end of a file Attempt to open a non-existent fileAttempt to open a non-existent file Attempts to read beyond the end of an arrayAttempts to read beyond the end of an array etc.etc.

Page 14: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

14

ExceptionsExceptions When a potential error condition occurs while a When a potential error condition occurs while a

program is running an program is running an ExceptionException occurs occurs For example attempting to read from a non-existent For example attempting to read from a non-existent

filefile Exceptions are of specific types - e.g.Exceptions are of specific types - e.g.

ArithmeticExceptionArithmeticException IOExceptionIOException ArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException

If the program contains code to handle the If the program contains code to handle the exception that code is triggered.exception that code is triggered.

If there is no code to handle the exception then If there is no code to handle the exception then the program terminates with a runtime error the program terminates with a runtime error

Page 15: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

15

Missing Class FileMissing Class File

This can be either a compiler error This can be either a compiler error oror a runtime a runtime error!error!

If a class file used by the program is missing at If a class file used by the program is missing at compile time, then a compiler error is generatedcompile time, then a compiler error is generated

If the program is successfully compiled, but a If the program is successfully compiled, but a class file is missing when it is run, then a run-class file is missing when it is run, then a run-time error is generatedtime error is generated NoClassDefFoundErrorNoClassDefFoundError

Page 16: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

16

Null Pointer ExceptionsNull Pointer Exceptions

These are generated if your program atempts to These are generated if your program atempts to access something non-existent in memoryaccess something non-existent in memory

Internally these are drastic, but they can be Internally these are drastic, but they can be triggered by extremely subtle errorstriggered by extremely subtle errors

They do not always provide a line number (or They do not always provide a line number (or that line might not be where the error is)that line might not be where the error is)

They can be extremely hard to debugThey can be extremely hard to debug

Page 17: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

17

Logical ErrorsLogical Errors

The program compiles and runs, but it doesn’t The program compiles and runs, but it doesn’t do what is intendeddo what is intended

May be caused by:May be caused by: Some syntax errors (i.e. legal Java that Some syntax errors (i.e. legal Java that

is wrong in the current context)is wrong in the current context) Errors in logical designErrors in logical design

These are “bugs”These are “bugs” Term coined by Admiral Grace Term coined by Admiral Grace

Hopper for anything that causes Hopper for anything that causes a program to do something a program to do something unexpectedunexpected

Page 18: School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging

18

DebuggingDebugging

Paper helps - examine your flow chartsPaper helps - examine your flow charts Code toolsCode tools

Flags - code that tells you where you are in a programFlags - code that tells you where you are in a program Breaks - code that stops the program Breaks - code that stops the program Watches - code that prints the contents of variablesWatches - code that prints the contents of variables

DebuggersDebuggers Software that implements the aboveSoftware that implements the above Also runs code visually in “slow motion”Also runs code visually in “slow motion” Built into most integrated development environmentsBuilt into most integrated development environments Invaluable for debugging large, complex programsInvaluable for debugging large, complex programs