Java

Exception Handling

14 lessons in this section of the Java tutorial. Work through them in order, or jump to the one you need.

  1. 1.Exception Basics

    An exception is an event that disrupts the normal flow of a program during execution.

  2. 2.try-catch

    The try-catch statement is Java's primary mechanism for handling exceptions.

  3. 3.Multiple catch

    A single try block can be followed by multiple catch blocks, each handling a different exception type.

  4. 4.finally

    The finally block contains code that must execute regardless of whether the try block completed normally, threw an exception that was caught, or threw an exception that…

  5. 5.throw

    The throw statement explicitly throws an exception object, immediately terminating normal execution and initiating exception propagation.

  6. 6.throws

    The throws clause in a method signature declares that the method may throw the listed checked exceptions.

  7. 7.Custom Exception

    A custom exception is a user-defined exception class that extends Exception, RuntimeException, or one of their subclasses.

  8. 8.Checked Exception

    A checked exception is an exception that the Java compiler requires you to either handle with a try-catch block or declare with a throws clause on the method signature.

  9. 9.Unchecked Exception

    An unchecked exception is an exception that the Java compiler does not require you to handle or declare.

  10. 10.Error vs Exception

    Java's throwable hierarchy has two branches under Throwable: Exception and Error.

  11. 11.Stack Trace

    A stack trace is the recorded sequence of method calls that were active at the moment an exception was thrown.

  12. 12.try-with-resources

    Try-with-resources is a Java 7 feature that automatically closes resources when a try block exits, whether normally or due to an exception.

  13. 13.Exception Propagation

    Exception propagation is the mechanism by which a thrown exception travels up the call stack when no handler is found at the point of throwing.

  14. 14.Chained Exceptions

    Exception chaining is the practice of associating a caught exception with a new exception being thrown, preserving the full causal history of a failure.