-
Hajipur, Bihar, 844101
In every programming language, errors are a normal part of development. They help identify what went wrong in the program and guide the programmer toward fixing it. In Java, errors occur when something prevents the program from running correctly — like missing syntax, invalid logic, or system-level failures.
In this tutorial, you’ll learn about the different types of errors in Java, how they occur, and how to handle or prevent them effectively.
An error in Java is any unexpected condition that stops a program from compiling or executing correctly. Errors can occur during compilation or runtime depending on the nature of the issue.
There are mainly three types of errors in Java:
Syntax Errors (Compile-time Errors)
Runtime Errors
Logical Errors
Let’s understand each type in detail.
Syntax errors happen when you write code that doesn’t follow Java’s grammar or rules. These are detected by the Java compiler before the program runs.
Common syntax errors include:
Missing semicolons
Incorrect variable names
Missing brackets or parentheses
Using undeclared variables
class Example {
public static void main(String[] args) {
System.out.println("Hello World")
}
}
Error: Missing semicolon at the end of the println() statement.
The compiler will display:
error: ';' expected
You must fix all syntax errors before your program can compile successfully.
Runtime errors occur while the program is running. These errors cause the program to stop unexpectedly or behave incorrectly. They usually happen due to invalid input, file handling issues, or memory problems.
Some common runtime errors are:
Dividing by zero
Accessing an invalid array index
Using a null object reference
Opening a file that doesn’t exist
class Example {
public static void main(String[] args) {
int number = 10 / 0;
System.out.println("Result: " + number);
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
The compiler won’t detect this issue, but it will crash at runtime.
Logical errors don’t cause the program to crash, but they produce incorrect output. These are the hardest to find because the syntax is correct, and the program runs — it just gives wrong results.
class Example {
public static void main(String[] args) {
int a = 10, b = 5;
int sum = a - b; // Logic mistake, should be a + b
System.out.println("Sum: " + sum);
}
}
Output will be Sum: 5 instead of Sum: 15.
Since no error message appears, logical mistakes require debugging and careful testing to find.
Apart from the basic error types, Java also classifies errors based on their severity:
Detected by the compiler (e.g., syntax errors, missing imports, incorrect method calls).
Occur while the program executes (e.g., dividing by zero, null pointer exceptions).
Caused by incorrect logic or formulas in your code.
These occur due to hardware or system-level issues, such as running out of memory or JVM crashing.
| Error Type | Example | Description |
|---|---|---|
| Syntax Error | Missing semicolon | Compiler fails to compile |
| Runtime Error | Divide by zero | Program stops while running |
| Logical Error | Wrong calculation | Output is incorrect |
| Null Pointer | Object not initialized | Program crashes |
| Array Index | Accessing invalid index | Out of bounds exception |
While you can’t completely avoid all errors, you can reduce them by following some best practices:
Use meaningful variable names — helps avoid logical confusion.
Test your code frequently — find and fix issues early.
Add validation for user input — avoid runtime failures.
Use IDE features — modern IDEs highlight syntax and runtime warnings.
Write clean, modular code — easier to debug and test.
It’s important to note that errors and exceptions are not the same in Java.
| Basis | Error | Exception |
|---|---|---|
| Origin | System or environment issues | Caused by program or user |
| Recoverable | Usually not recoverable | Can be handled with try-catch |
| Example | OutOfMemoryError, StackOverflowError | IOException, ArithmeticException |
Errors are more serious and often indicate something that cannot be fixed while the program is running. Exceptions, on the other hand, can often be caught and handled gracefully.
In this tutorial, you learned what errors are in Java, their main types (syntax, runtime, and logical), and how they differ from exceptions. You also saw how to identify and prevent common mistakes that can break or mislead your program.
Understanding errors is the first step before learning debugging and exception handling — two powerful tools that help you write cleaner, error-free Java programs.
Write a Java program that intentionally creates a syntax error and then fix it to make the program run correctly.
Create a program that causes a runtime error by dividing a number by zero.
Write a Java program that produces a logical error while calculating the average of three numbers.
Create a program that generates a NullPointerException and explain why it happens.
Write a Java program that demonstrates an ArrayIndexOutOfBoundsException by accessing an invalid array index.
Create a simple program that checks for input validation before performing division to prevent runtime errors.
Write a Java program where a user enters two integers, and handle the case where the second number is zero to avoid errors.
Create a program that demonstrates a compile-time error by using an undeclared variable.
Write a Java program that performs subtraction instead of addition (a logical mistake), then fix it.
Create a program that prints different types of errors and exceptions using comments to explain each one.