You are on page 1of 2

Lesson 7

Part 2

Types of Errors in a Program

1. Syntax Error – This type of error occurs due to incorrect use of the syntax
or format of the programming language and is identified during the
compilation of the code.

Eg:

system.out.Println(JAVA”)

In the above statement following are the syntax errors:

a. “ s ” is in lower case
b. “ P ” is in upper case
c. Opening double quotes is missing
d. Semicolon is missing

2. Logical Error – This type of error occurs due wrong logic of the code which
results in incorrect or unexpected output is not identified either during
compilation nor during execution of the code.

Eg:

Suppose we need to find the average of 3 numbers(a, b and c) and we write


the code as:

int avg = a + b + c / 3 ;

The above line will not generate any error but gives us an unexpected
output. In this case instead of average we get the addition of a, b and c/3.

The correct form will be:

int avg = (a + b + c) / 3 ;

3. Runtime Error – This type of error occurs and is identified during the
execution of a program.

Eg:

a. Divide by Zero – It arises where an expression is divided by 0


b. Array Index Out of Bounds – It occurs when we try to access an array
index which doesn’t exist.
Comments in Java

The Java comments are the statements that are not executed by the compiler and
interpreter. The comments can be used to provide information or explanation about
the variable, method, class or any statement. It can also be used to hide program
code.

In Java there are three types of comments:


1. Single line comment - This type of comment is mainly used for describing
the code functionality. Single-line comments start with two forward slashes
(//). Any text between // and the end of the line is ignored by Java implies it
will not be executed.

2. Multi line comment - To describe a full method in a code or a complex


block of code single line comments can be tedious to write since we have to
give ‘//’ at every line. So to overcome this multi-line comments can be
used. Multi-line comments start with /* and end with */. Any text between
/* and */ will be ignored by Java. It is used to comment multiple lines of
code.

3. Documentation comment - This type of comments is used generally


when you are writing code for a project or software package. It helps you to
generate a documentation page for reference, which can be used for getting
information about methods present, its parameters, etc. It starts with /**
and ends with */.

You might also like