public class StackErrorExample
{
public static void main(String[] args)
{
main(null); // infinite recursive call
}
}
import java.util.ArrayList;
public class MemoryErrorExample
{
public static void main(String[] args)
{
ArrayList list = new ArrayList<>();
while (true)
{
list.add(new int[1000000]); // keeps consuming memory
}
}
}
Errors in Java can be broadly divided into three main categories
statc
instead of static
, viod
instead of void
.
;
, wrong variable declaration, wrong method declaration/calling.
int x = "hello";
String
to an int
.
StackOverflowError
OutOfMemoryError
try-catch
.
ArithmeticException
β divide by zero
NullPointerException
β accessing null object
ArrayIndexOutOfBoundsException
β invalid array index
// Example of Logical Error
public class LogicalErrorExample
{
public static void main(String[] args)
{
int side = 5;
int area = 4 * side; // Wrong formula
System.out.println("Area = " + area); // Incorrect result
}
}
Error
is the pre-defined class in Java which inherits the Throwable
class.
Error
class in Java.
Object
class is the parent class of all the classes in Java
Throwable
class is the parent class of Error
class in Java.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.