Page 84 - computer science (868) class 11
P. 84
3.6.1 Types of Errors
There are three types of errors which are as follows:
• Syntax error
• Logical error
• Runtime error
Syntax Error or Compile Time Error
While coding, a grammatical error leads to a syntax error. The compiler stops the execution of the program as soon as
it encounters a syntax error. So, the code needs to be corrected before execution.
Let us consider a program to find the area of a rectangle.
class syntax_error
{
void calculate(int l, b)
{
int ar = l * b;
System.out.println("Area is " ar);
}
}
In the above example, there are two syntax errors:
• calculate(int l, b), there is no “int” before b.
• System.out.println(“Area is” ar); here before ar, there should be + to concatenate the two strings.
If these errors are removed, then the program will compile.
Logical Error
There are some errors that will execute the program but the desired output is not achieved. This type of error is called
logical error.
Let us consider a program to print the sum of first 10 natural numbers.
class logical_error
{
void natural()
{
int i, s = 1;
for(i=1; i<10; i++)
{
s = s+i;
}
System.out.println("Sum "+s);
}
}
In the above example, there are two logical errors:
• “s” should be initialised with 0.
• The sum of first 9 naturals numbers will be printed instead of 10 as there is “<10”. It should be “<=10”. But this type
of a mistake cannot be recongnised by the compiler.
Runtime Error
The error that occurs during the execution of a successfully compiled program, is a runtime error. If the user enters
wrong data or some data that is not required, a runtime error occurs. They can be detected during the process of
debugging.
Let us consider a program to print the quotient of two numbers.
8282 Touchpad Computer Science-XI

