CMP202 Lecture 3 - Errors and Debugging
CMP202 Lecture 3 - Errors and Debugging
II
LECTURE SERIES
OVERVIEW OF THE COURSE OUTLINE
Principles of Good programming
Structured programming concepts
Errors and Debugging
Testing
Text Files and IO
WHAT ARE ERRORS?
• Errors are the problems or the faults that occur in the program,
which makes the behavior of the program abnormal. Even
experienced developers can also make these faults.
• Programming errors are also known as the bugs or faults, and the
process of removing these bugs is known as debugging.
• Debugging is the process of finding and correcting existing and
potential errors in software code. Bugs can cause software code to
behave unexpectedly or crash.
• These errors are detected either during the time of compilation or
execution. Thus, the errors must be removed from the program for
the successful execution of the program.
TYPES OF ERRORS
• Syntax errors
• Runtime errors
• Logical errors
• Semantic errors
• Compilation errors
• Arithmetic errors
• Resource errors
• Interface Errors
. SYNTAX ERRORS
• Syntax errors occur when syntactical problems occur in a certain
programming language due to incorrect use syntax.
• When you first get started with coding, you will probably make lots of
syntax errors. These are errors that occur when you have not
followed the rules of the language, and they will be flagged by the
translator. A program with syntax errors will not run.
• Most IDEs (integrated development environments) highlight syntax
errors when you try to run your code
----- SYNTAX ERRORS
• Programming languages such as Python or java are very simple
compared to natural languages such as English, Spanish or Japanese.
However, they do have a lot of things in common:
• it matters what order the words are in;
• they both have syntax (that is, a grammar) that define how the words can be
combined together;
• you have to spell words correctly;
• you can translate between one programming language and another;
• they both use punctuation to structure and organise words and sentences;
• there are multiple ways of writing the same code or “paragraph” to describe
the same thing.
---- SYNTAX ERRORS
• These syntactical problems may be:
• Missing semicolons,
• Missing brackets,
• Misspelled keywords,
• Use of undeclared variables,
• Improperly named variable or function or class,
• Class not found,
• Missing double-quote in Strings, etc.
How Syntax errors are detected?
• Syntax errors are easy to spot and rectify because the Java compiler
finds them for you.
• The compiler will tell you which piece of code in the program got in
trouble and its best guess as to what you did wrong. Usually, the
compiler indicates the exact line where the error is, or sometimes the
line just before it.
• These errors are detected by the compiler at compile time of the
program which is why they are also known as compile-time errors.
• When the compiler encounters syntax errors in a program, it prevents
the code from compiling successfully and will not create a .class file
until errors are not corrected. An error message will be displayed on
the output screen.
Example of Syntax Error: Missing semicolon
Consider the following code.
In the above code the closing curly bracket of the main() method is
missing.
Example of Syntax Error: Misspelled Keyword
Consider the following code.
• In the above code the Java compiler is throwing multiple errors because it is
not considering the "Hello World!" as a String as it is missing the double
quotes.
RUNTIME ERRORS
• Runtime errors occur when the program has successfully compiled without
giving any errors and creating a ".class" file.
• However, the program does not execute properly. These errors are detected at
runtime or at the time of execution of the program.
• The Java compiler does not detect runtime errors because the Java compiler
does not have any technique to catch these errors as it does not have all the
runtime information available to it. Runtime errors are caught by Java Virtual
Machine (JVM) when the program is running.
• These runtime errors are called exceptions and they terminate the program
abnormally, giving an error statement.
• Exceptions are errors thrown at runtime.
• We can use exception handling techniques in Java to handle these runtime errors.
• In exception handling, the piece of code the programmer thinks can produce the error
is put inside the try block and the programmer can catch the error inside
the catch block.
What causes Run time Errors?
• The programmer wants to print even numbers but, instead of using a modulus
operator (%) he/she uses a divide operator (/).
• The actual output and the expected output do not match which means the
programmer has committed a logical error somewhere in the program. We wanted
to print even numbers, but we got 0 and 1 because we were using "/" and not "%".
SEMANTIC ERRORS
• A semantic error has to do with meaning. If a program contains this
kind of error, it will successfully run, but won’t output the correct
result. Debugging is not that easy with a semantic error.
• A semantic error occurs when a statement is syntactically valid, but
does not do what the programmer intended.
• Modern compilers have been getting better at detecting certain types
of common semantic errors (e.g. use of an uninitialized variable).
• The semantic error can arises using the wrong variable or using wrong
operator or doing operation in wrong order.
Example of Semantic Error: Use of a non-initialized variable
• Consider the following code: