Exception Types in java

In Java, exceptions are categorized into two main types: checked exceptions and unchecked exceptions. Additionally, there is a special category called errors. Here’s an overview of these exception types:

  1. Checked Exceptions:
  • Checked exceptions are exceptions that are checked at compile time.
  • These exceptions must be either caught using a try-catch block or declared in the method signature using the throws keyword.
  • Examples of checked exceptions include IOException, SQLException, and ClassNotFoundException.
   try {
       // code that may throw a checked exception
   } catch (IOException e) {
       // handle the IOException
   }

or

   void someMethod() throws IOException {
       // code that may throw a checked exception
   }
  1. Unchecked Exceptions:
  • Unchecked exceptions, also known as runtime exceptions, are exceptions that are not checked at compile time.
  • They typically result from programming errors and are subclasses of RuntimeException or its subclasses.
  • Examples of unchecked exceptions include NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException.
   // Unchecked exception (not explicitly caught or declared)
   int result = 10 / 0; // ArithmeticException
  1. Errors:
  • Errors represent abnormal conditions that a reasonable application should not try to catch.
  • They are typically severe issues that indicate a problem with the environment or system, rather than the application itself.
  • Examples of errors include OutOfMemoryError and StackOverflowError.
   // Example of an error
   throw new OutOfMemoryError("Insufficient memory to continue execution");

It’s important to handle exceptions appropriately in your code to ensure robust error management. Checked exceptions force developers to either handle the exception or explicitly declare that the method might throw the exception, while unchecked exceptions are often related to programming errors and should be addressed through proper coding practices. Errors, on the other hand, are usually beyond the control of the application and may indicate serious problems with the environment.

All exception types are subclasses of the built-in class Throwable. Thus, Throwable is at the top of the exception class hierarchy

Throwable are two subclasses that partition exceptions into two distinct branches. One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. This is also the class that you will subclass to create your own custom exception types. There is an important subclass of Exception, called RuntimeException. Exceptions of this type are automatically defined for the programs that you write and include things such as division by zero and invalid array indexing.

The other branch is topped by Error, which defines exceptions that are not expected to be caught under normal circumstances by your program. Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself

Stack overflow is an example of such an error.

Leave a Comment