Search⌘ K
AI Features

Exceptions Handling

Explore how to handle exceptions in Java effectively by understanding the use of checked versus runtime exceptions. Learn the importance of exception translation, chaining, and writing failure atomic code. This lesson helps you improve Java reliability and maintainability by following best practices for throwing, catching, and documenting exceptions.

We'll cover the following...

Notes on Exceptions

  1. Use exceptions for exceptional situations and not for ordinary control flow.

  2. Use checked exceptions for conditions from which the caller can reasonably be expected to recover. Checked exceptions force the programmer to deal with exceptional conditions, greatly enhancing reliability.

  3. Runtime exceptions should be used to indicate programming errors only.

  4. Don't subclass Error to create new subtypes. The general convention is that errors are reserved for use by the JVM to indicate resource deficiencies, invariant failures, or other conditions that make it impossible to continue execution. Given this convention, it’s best not to implement any new Error subclasses. Therefore, all of the unchecked throwables needed should subclass RuntimeException.

  5. Favor the use of standard exceptions rather than creating new ones to make code easier to comprehend and familiar.

    Exception NameUsed For

    IllegalArgumentException

    Throw this exception when a method is invoked with an ...