Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging."— Presentation transcript:

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

2 2 Types of Errors Syntax errors Syntax errors Mistakes in the rules of the language itself Mistakes in the rules of the language itself Illegal Java - errors of grammar or punctuation Illegal Java - errors of grammar or punctuation Semantic errors Semantic errors Mistakes in the logic of the code Mistakes in the logic of the code Legal Java - but not what you intend! Legal Java - but not what you intend!

3 3 Where errors show themselves Compile-time errors Compile-time errors Many syntax errors are detected by the compiler Many syntax errors are detected by the compiler The compiler will generate an error message - including a line number The compiler will generate an error message - including a line number Run-time errors Run-time errors Some syntax errors are detected by the VM when the program is run Some syntax errors are detected by the VM when the program is run Legal Java - that causes problems under certain conditions. Legal Java - that causes problems under certain conditions. Logical errors Logical errors Unexpected results - bugs! Unexpected results - bugs!

4 4 Compiler Errors If javac gives no output, the compilation is successful If javac gives no output, the compilation is successful Cannot read… compiler error Cannot 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 command Usually a typo in the file name or javac command Remember Java is case sensitive! Remember Java is case sensitive! Could be file privileges Could be file privileges C:\code javac fname.java error: cannot read: fname.java 1 error C:\code

5 5 Compiler Errors (2) Most compiler errors have a file name and line number Most 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.java fname.java:23: cannot resolve symbol symbol : class string location: class fname string msg; ^ 1 error

6 6 Compiler Errors (cont.) Most compiler errors have a file name and line number Most 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.java fname.java:23: cannot resolve symbol symbol : class string location: class fname string msg; ^ 1 error File name (fname.java)

7 7 Compiler Errors (cont.) Most compiler errors have a file name and line number Most 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.java fname.java:23: cannot resolve symbol symbol : class string location: class fname string msg; ^ 1 error Line number (23)

8 8 Compiler Errors (cont.) Most compiler errors have a file name and line number Most 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.java fname.java:23: cannot resolve symbol symbol : class string location: class fname string msg; ^ 1 error Type of error

9 9 Compiler Errors (cont.) Most compiler errors have a file name and line number Most 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.java fname.java:23: cannot resolve symbol symbol : class string location: class fname string msg; ^ 1 error Type of error Details of error

10 10 Compiler Errors (cont.) Most compiler errors have a file name and line number Most 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.java fname.java:23: cannot resolve symbol symbol : class string location: class fname string msg; ^ 1 error Location of error

11 11 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 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 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 Compiler errors can have knock-on effects, with other “spurious” errors being caused by the first ADVICE - fix the first compiler error, and attempt to compile before looking at others - they might disappear! ADVICE - fix the first compiler error, and attempt to compile before looking at others - they might disappear!

12 12 Runtime Errors Runtime errors occur while the program is running, although the compilation is successful Runtime errors occur while the program is running, although the compilation is successful Usually runtime errors consist of “exceptions” in a thread Usually 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” NoClassDefFoundError NoClassDefFoundError Caused if the interpreter can’t find the named class file (e.g. fname.class) 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 This is usually a typo - either in the command line, the class declaration or the file name Remember Java is case sensitive! Remember Java is case sensitive! Exception in thread "main" java.lang.NoClassDefFoundError: fname

13 13 Causes of Runtime Errors Errors that only become apparent during the course of execution of the program Errors that only become apparent during the course of execution of the program External Factors - e.g. External Factors - e.g. Out of memory Out of memory Hard disk full Hard disk full Insufficient i/o privileges Insufficient i/o privileges etc. etc. Internal Factors - e.g. Internal Factors - e.g. Arithmetic errors Arithmetic errors Attempts to read beyond the end of a file Attempts to read beyond the end of a file Attempt to open a non-existent file Attempt to open a non-existent file Attempts to read beyond the end of an array Attempts to read beyond the end of an array etc. etc.

14 14 Exceptions When a potential error condition occurs while a program is running an Exception occurs When a potential error condition occurs while a program is running an Exception occurs For example attempting to read from a non-existent file For example attempting to read from a non-existent file Exceptions are of specific types - e.g. Exceptions are of specific types - e.g. ArithmeticException ArithmeticException IOException IOException ArrayIndexOutOfBoundsException ArrayIndexOutOfBoundsException If the program contains code to handle the exception that code is triggered. 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 If there is no code to handle the exception then the program terminates with a runtime error

15 15 Missing Class File This can be either a compiler error or a runtime error! 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 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 run- time error is generated If the program is successfully compiled, but a class file is missing when it is run, then a run- time error is generated NoClassDefFoundError NoClassDefFoundError

16 16 Null Pointer Exceptions These are generated if your program atempts to access something non-existent in memory 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 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 do not always provide a line number (or that line might not be where the error is) They can be extremely hard to debug They can be extremely hard to debug

17 17 Logical Errors The program compiles and runs, but it doesn’t do what is intended The program compiles and runs, but it doesn’t do what is intended May be caused by: May be caused by: Some syntax errors (i.e. legal Java that is wrong in the current context) Some syntax errors (i.e. legal Java that is wrong in the current context) Errors in logical design Errors in logical design These are “bugs” These are “bugs” Term coined by Admiral Grace Hopper for anything that causes a program to do something unexpected Term coined by Admiral Grace Hopper for anything that causes a program to do something unexpected

18 18 Debugging Paper helps - examine your flow charts Paper helps - examine your flow charts Code tools Code tools Flags - code that tells you where you are in a program Flags - 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 variables Watches - code that prints the contents of variables Debuggers Debuggers Software that implements the above Software that implements the above Also runs code visually in “slow motion” Also runs code visually in “slow motion” Built into most integrated development environments Built into most integrated development environments Invaluable for debugging large, complex programs Invaluable for debugging large, complex programs


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

Similar presentations


Ads by Google