0% found this document useful (0 votes)
36 views27 pages

CMP202 Lecture 3 - Errors and Debugging

Uploaded by

Akorede Bashir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views27 pages

CMP202 Lecture 3 - Errors and Debugging

Uploaded by

Akorede Bashir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

CMP 202- COMPUTER PROGRAMMING

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, semicolon is missing at the end of the int b =


4b=4 statement.
Example of Syntax Error: Missing bracket
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 System keyword in System.out.println() name is


misspelled as a system.
• Lower cased!
Example of Syntax Error: Cannot find a symbol
Consider the following code.

• In the above code, we have not imported the java.util.Scanner package


and using Scanner to take integer input. This has caused the error.
Example of Syntax Error: Missing double-quote in String
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?

• A runtime error can happen when:


• Dividing an integer by zero.
• Trying to store a value into an array that is not compatible type.
• Trying to access an element that is out of range of the array.
• Passing an argument that is not in a valid range or valid value for a method.
• Striving to use a negative size for an array.
• Attempting to convert an invalid string into a number.
Example of Run time error: Accessing array index that does not exists

• Consider the following code:

• When we try to access an array index that is out of bounding of the


size of the array(in this case it's 5), we get runtime error of array out
of bound.
Example of Run time error: Dividing an integer by zero
• Consider the following code:

• We cannot divide anything from zero so when we try to do that we


get an arithmetic exception at runtime.
Example of Run time error: Using negative size of an array
• Consider the following code:

• We cannot have a negative-sized array, it is just not possible. We get a


Negative array size exception when we try to do that.
LOGICAL ERRORS
• Logical errors are the hardest to identify and rectify.
• They are hardest to detect because they are neither identified by the
Java compiler nor by the JVM.
• The programmer is entirely responsible for them.
• The program with a logical error will get compiled and executed
successfully but will not give the expected result.
• Logical errors can be detected by application testers when they
compare the actual result with the program's expected result.
What causes Logical Errors
• These type of errors occur due to the following reasons:
• Not correctly understanding what the program needed to do
• Using the incorrect logical operator in a selection statement.
• Missing or incorrect positioning of brackets in mathematical calculations, which means
that incorrect result is returned.
• Loops that execute more or fewer times than intended.

• Debugging logic errors often involves tracking the values of variables,


as code is executed step by step.
• Many IDE’s offer a “Watch window” that allows the programmer to
specify one or more variables to watch.
• Dry running, which is where the programmer goes through the code
line by line using a trace table is a manual approach to the same task.
Example of Logical Errors: Program to print even
numbers
• Consider the following code:

• 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:

• The variable is not initialized, therefore cannot be incremented!


Example of Semantic Error: Type incompatibility
• Consider the following code:

• The variable is of type integer and a string is provided!


• Types String and int are not compatible!
Example of Semantic Error: Errors in expressions
• Consider the following code:

• The operation string to be subtracted from integer is incorrect!


Example of Semantic Error: Unknown references
• Consider the following code:
Example of Semantic Error: Array index out of range

• Consider the following code:

• This kind of error is called dynamic semantic error

You might also like