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
Use exceptions for exceptional situations and not for ordinary control flow.
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.
Runtime exceptions should be used to indicate programming errors only.
Don't subclass
Errorto 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 newErrorsubclasses. Therefore, all of the unchecked throwables needed should subclassRuntimeException.Favor the use of standard exceptions rather than creating new ones to make code easier to comprehend and familiar.
Exception Name Used For IllegalArgumentExceptionThrow this exception when a method is invoked with an ...