Errors in Java are problems that occur during the compilation or execution of a program. These errors can prevent the program from running correctly or produce unexpected results. Understanding different types of errors helps developers debug programs and write reliable applications.
- Some errors stop program execution, while others produce incorrect output.
- Learning about errors helps improve debugging and problem-solving skills.
Types of Errors
The errors can be broadly classified into three categories: Runtime Errors, Compile-Time Errors, and Logical Errors. Each type of error has distinct characteristics and requires specific methods for detection and resolution.

1. Run Time Error
Runtime errors occur during the execution of a program after successful compilation. These errors arise when the program performs an illegal operation or receives invalid input. They are detected by the JVM while the program is running.
- Occur due to conditions like division by zero, invalid input, or memory issues.
- Can be handled using exception handling (try-catch blocks).
- May cause the program to terminate abruptly if not handled properly.
Example 1: Runtime Error caused by dividing by zero
class DivByZero {
public static void main(String args[])
{
int var1 = 15;
int var2 = 5;
int var3 = 0;
int ans1 = var1 / var2;
// This statement causes a runtime error,
// as 15 is getting divided by 0 here
int ans2 = var1 / var3;
System.out.println(
"Division of va1"
+ " by var2 is: "
+ ans1);
System.out.println(
"Division of va1"
+ " by var3 is: "
+ ans2);
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at DivByZero.main(File.java:14)
Example 2: Runtime Error caused by Assigning/Retrieving Value from an array using an index which is greater than the size of the array.
class RTErrorDemo {
public static void main(String args[])
{
int arr[] = new int[5];
// Array size is 5
// whereas this statement assigns
// value 250 at the 10th position.
arr[9] = 250;
System.out.println("Value assigned! ");
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at RTErrorDemo.main(File.java:10)
2. Compile Time Error
Compile-time errors occur during the compilation phase due to incorrect syntax or structure in the code. These errors prevent the program from running and are identified by the compiler.
- Caused by missing semicolons, incorrect syntax, or undeclared variables.
- The compiler provides error messages with line numbers for easy debugging.
- Must be fixed before the program can be executed
Example 1: Misspelled variable name or method names
class MisspelledVar {
public static void main(String args[])
{
int a = 40, b = 60;
// Declared variable Sum with Capital S
int Sum = a + b;
// Trying to call variable Sum
// with a small s ie. sum
System.out.println(
"Sum of variables is "
+ sum);
}
}
Output:
prog.java:14: error: cannot find symbol
+ sum);
^
symbol: variable sum
location: class MisspelledVar
1 error
Example 2: Missing semicolons
class PrintingSentence {
public static void main(String args[])
{
String s = "GeeksforGeeks";
// Missing ';' at the end
System.out.println("Welcome to " + s)
}
}
Output:
prog.java:8: error: ';' expected
System.out.println("Welcome to " + s)
^
1 error
Example: Missing parenthesis, square brackets, or curly braces
class MissingParenthesis {
public static void main(String args[])
{
System.out.println("Printing 1 to 5 \n");
int i;
// missing parenthesis, should have
// been for(i=1; i<=5; i++)
for (i = 1; i <= 5; i++ {
System.out.println(i + "\n");
} // for loop ends
} // main ends
} // class ends
Ouput:
prog.java:10: error: ')' expected
for (i = 1; i <= 5; i++ {
^
1 error
Example: Incorrect format of selection statements or loops
class IncorrectLoop {
public static void main(String args[])
{
System.out.println("Multiplication Table of 7");
int a = 7, ans;
int i;
// Should have been
// for(i=1; i<=10; i++)
for (i = 1, i <= 10; i++) {
ans = a * i;
System.out.println(ans + "\n");
}
}
}
Output:
prog.java:12: error: not a statement
for (i = 1, i <= 10; i++) {
^
prog.java:12: error: ';' expected
for (i = 1, i <= 10; i++) {
^
2 errors
3. Logical Error
Logical errors occur when the program compiles and runs successfully but produces incorrect output. These errors are caused by mistakes in the program’s logic or algorithm.
- Do not generate any error message, making them harder to detect.
- Occur due to wrong conditions, formulas, or operators used in code.
- Can be identified using testing, debugging, and code review.
Example: Accidentally using an incorrect operator on the variables to perform an operation (Using '/' operator to get the modulus instead using '%')
public class LErrorDemo {
public static void main(String[] args)
{
int num = 789;
int reversednum = 0;
int remainder;
while (num != 0) {
// to obtain modulus % sign should
// have been used instead of /
remainder = num / 10;
reversednum
= reversednum * 10
+ remainder;
num /= 10;
}
System.out.println("Reversed number is "
+ reversednum);
}
}
Output
Reversed number is 7870
Example: Displaying the wrong message
class IncorrectMessage {
public static void main(String args[])
{
int a = 2, b = 8, c = 6;
System.out.println(
"Finding the largest number \n");
if (a > b && a > c)
System.out.println(
a + " is the largest Number");
else if (b > a && b > c)
System.out.println(
b + " is the smallest Number");
// The correct message should have
// been System.out.println
//(b+" is the largest Number");
// to make logic
else
System.out.println(
c + " is the largest Number");
}
}
Output
Finding the largest number 8 is the smallest Number