Exception Hierarchy in java

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 85:-  Exception Hierarchy in java

In Java, the exception hierarchy is organized as a class hierarchy, where different types of exceptions are represented by different classes. All exception classes in Java are derived from the Throwable class, which serves as the root of the exception hierarchy. The Throwable class has two main subclasses: Error and Exception.

  1. Error:
    • Errors are exceptional conditions that generally cannot be handled by the application. They are typically caused by serious problems that are beyond the control of the programmer, such as system failures, out of memory errors, etc.
    • Examples of errors: OutOfMemoryError, StackOverflowError, NoSuchMethodError, AssertionError, etc.
  2. Exception:
    • Exceptions represent exceptional conditions that can be handled and recovered from within the application. They occur during the normal execution of the program and may be caused by various factors, such as invalid inputs, I/O errors, arithmetic errors, etc.
    • Exceptions are further divided into two subtypes: checked exceptions and unchecked exceptions.
    • Checked Exceptions:
      • Checked exceptions must be either caught using a try-catch block or declared to be thrown in the method signature. These exceptions are known to be recoverable, and the compiler enforces their handling.
      • Examples of checked exceptions: IOException, SQLException, ParseException, InterruptedException, etc.
    • Unchecked Exceptions:
      • Unchecked exceptions, also known as runtime exceptions, do not need to be explicitly caught or declared. They usually indicate programming errors or logical issues within the application.
      • Examples of unchecked exceptions: NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException, IllegalArgumentException, etc.

Below is a simplified representation of the Java exception hierarchy:

phpCopy code

Throwable    ├── Error    │     ├── OutOfMemoryError    │     ├── StackOverflowError    │     ├── ...    │    └── Exception          ├── RuntimeException (Unchecked)          │     ├── NullPointerException          │     ├── IndexOutOfBoundsException          │     ├── ArithmeticException          │     ├── ...          │          └── IOException (Checked)                ├── FileNotFoundException                ├── SocketException                ├── ...

When writing Java code, you need to handle or declare checked exceptions explicitly, while unchecked exceptions usually indicate issues that need to be resolved in the code. By understanding the exception hierarchy, you can better manage exceptional situations and make your programs more robust and reliable.

12. Advanced

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support