[post-views]
In this lesson, you will learn
Errors are mistakes or faults that can make a program go wrong and may terminate the program execution abruptly.
In Java, the errors can generally be classified into two main categories: compile-time errors and run-time errors.
Compile-time errors occur during the compilation phase of your program before the Java Virtual Machine (JVM) begins execution.
Compile-time errors typically include syntax errors, type-checking errors, and any other errors that can be detected during the compilation of the program.
The following are the common compile time errors.
1. Syntax errors: Mistakes in the code that violate the grammar rules of the Java language.
public class CompileTimeExample {
public static void main(String[] args) {
System.out.println("Hello, world!") // Missing semicolon
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert ";" to complete BlockStatements
at stringdemo.CompileTimeExample.main(CompileTimeExample.java:6)
2. Type-checking errors: Attempting to operate on a data type that is not allowed.
public class CompileTimeExample {
public static void main(String[] args) {
int number = "123";
// Trying to assign a String to an integer variable
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from String to int
at stringdemo.CompileTimeExample.main(CompileTimeExample.java:6)
3. Undeclared variable: Trying to use a variable that has not been declared.
public class CompileTimeExample {
public static void main(String[] args) {
x = 100; // x is not declared
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
x cannot be resolved to a variable
at stringdemo.CompileTimeExample.main(CompileTimeExample.java:6)
Run-time errors occur during the program execution after the program is successfully compiled.
Run-time errors produce wrong results due to the wrong logic and abruptly terminate the flow of the program.
Note: These errors are usually more difficult to debug since they often depend on the program’s input or the current state of the program.
The following are the common runtime errors.
1. Division by zero: Attempting to divide a number by zero.
public class RunTimeExample {
public static void main(String[] args) {
int result = 10 / 0; // ArithmeticException is thrown
}
}
2. Accessing an out-of-bounds element in an array: Trying to access an array element with an invalid index.
public class RunTimeExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
System.out.println(numbers[3]); // ArrayIndexOutOfBoundsException
}
}
1. ArithmeticException
int result = 10 / 0; // Throws ArithmeticException
2. NullPointerException
null
in a case where an object is required.
String text = null;
int length = text.length(); // Throws NullPointerException
3. ArrayIndexOutOfBoundsException
int[] numbers = new int[5];
int number = numbers[5]; // Throws ArrayIndexOutOfBoundsException
ClassCastException
Object x = new Integer(0);
System.out.println((String)x); // Throws ClassCastException
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException
and StringIndexOutOfBoundsException
are more specific instances of IndexOutOfBoundsException
.
String str = "hello";
char ch = str.charAt(5); // Throws IndexOutOfBoundsException
IllegalArgumentException
Thread t = new Thread();
t.setPriority(11); // Throws IllegalArgumentException
IllegalStateException
Iterator<String> it = Arrays.asList("a", "b").iterator();
it.remove(); // Throws IllegalStateException
NoSuchElementException
Iterator<String> it = Arrays.asList().iterator();
it.next(); // Throws NoSuchElementException
NumberFormatException
int num = Integer.parseInt("abc"); // Throws NumberFormatException
StackOverflowError
public void recursivePrint(int num) {
System.out.println("Number: " + num);
recursivePrint(++num);
}
In the next lesson, you will learn how to handle exceptins raised in the program using the try-catch block.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.