Object-Oriented Programming with Java Lecture Notes

4 March 2008 • Exceptions


Outline

  • The error problem.

  • Exception basics.

    • throw, try, and catch.

  • The exception hierarchy.

    • Checked and unchecked exceptions.

  • Exception mechanism refinements.

black swan

Errors

The Error Pass-Along Problem

Error Return

Error Parameters

Error Globals

Exceptions

Exception Basics

Call-Stack Example.

Call-Stack Example..

Exceptions and the Call Stack

Stack Unwinding

Exception Problems

Basic Exception Mechanisms

The throw Statement

The catch Block

The try Block

Try-Catch Statements

Normal Try-Catch Control

  • Execute try-block code.

  • Control falls off the try-block end.

  • Execution resumes with the statement following the try-catch block.

    • The catch blocks are ignored.

normal try-catch control flow

Handled Exceptions

  • Jump from the try block to the catch blocks on exception.

  • Search for a matching catch block in order.

  • After executing the associated catch body, continue after the try-catch statement.

    Example

    Unhandled Exceptions

    • If no catch block matches, terminate the current method and continue unwinding the stack.

    • Assuming there's no nested try-catch statement involved.

    handled exception control flow

    Matching Exceptions

    The Exception Hierarchy

    The Throwable Ancestor

    The Error Class

    The Exception Class

    Exception Children

    The Exception Hierarchy

    Avoidable vs. Fateful Occurrences

    Handling Occurrences

    Checked vs. Unchecked Exceptions

    The throws Clause

    Javadoc and Exceptions

    Defining Exceptions

    Exception-Defining Tips

    Exception Clean-Up

  • Exceptions occur at inconvenient times.

    try { 
      // state set-up
      ci.g()
      // state tear-down
      }
    catch (...) { ... }
    catch (...) { ... }
    

  • ci.g()'s exception interrupts state tear-down.

    finally Blocks

    Finally Examples

    Summary

    Credits


    This page last modified on 4 March 2008.

    This work's CC license.