πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Errors in Java  


Introduction

  • An Error in Java is a serious problem that occurs at runtime (during the execution of a program).
  • Errors usually happen due to system-level issues (like memory shortage or JVM crash) rather than problems in the application logic.
  • Errors are beyond the control of the programmer and generally cannot be recovered from within the code.
  • For Example :
    • StackOverflowError
      • Occurs when there is deep or infinite recursion.
      • Program:
        public class StackErrorExample
        {
            public static void main(String[] args)
            {
                main(null); // infinite recursive call
            }
        }
    • OutOfMemoryError
      • Occurs when the JVM runs out of memory to allocate new objects.
      • Program:
        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
                }
            }
        }
    • VirtualMachineError
      • Thrown when the Java Virtual Machine (JVM) encounters a serious internal problem.
      • Example:
        • JVM crash due to insufficient resources.
        • Rare, but indicates JVM is in unstable state.

Types of Errors in Java

Errors in Java can be broadly divided into three main categories

  1. Compile-Time Errors
    • Errors that occur when the program is being compiled.
      • These are detected by the compiler before execution.
      • Program cannot run until these errors are fixed.
    • Examples :
      • Lexical Errors
        • Mistakes in keywords or identifiers.
        • Example: statc instead of static, viod instead of void.
      • Syntax Errors
        • Violation of Java grammar rules.
        • Example: Missing semicolon ;, wrong variable declaration, wrong method declaration/calling.
      • Semantic Errors
        • Code is syntactically correct but meaningless.
        • Example: int x = "hello";
      • Type Checking Errors
        • Mismatch of data types.
        • Example: Assigning a String to an int.
  2. Runtime Errors
    • Occur while the program is running (after compilation).
      • Caused by invalid operations at runtime.
    • These are of 2 types:
      • Errors (serious problems, not recoverable):
        • Thrown by JVM, usually not handled in programs.
        • Example:
          • StackOverflowError
          • OutOfMemoryError
      • Exceptions (recoverable problems):
        • Handled using try-catch.
        • Example:
          • ArithmeticException β†’ divide by zero
          • NullPointerException β†’ accessing null object
          • ArrayIndexOutOfBoundsException β†’ invalid array index
  3. Logical Errors (hardest to detect)
    • Program runs successfully but gives incorrect result due to wrong logic.
    • Its not detected by compiler or JVM.
    • For Example :
      // 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 Class Hierarchy

  • Error is the pre-defined class in Java which inherits the Throwable class.
  • Below is the hierarchy of Error class in Java.
Error Class Hierarchy in Java
  • Points to remember:
    • Object class is the parent class of all the classes in Java
    • Throwable class is the parent class of Error class in Java.
    • All the error classes are considered unchecked (because they are not checked by the compiler).