So, tons of exceptions have been declared without need for real polymorphism. Why? It’s because, something similar to polymorphism is being implemented by the caller.Does a sequence of catch clauses remind you of something?

} catch (ExceptionA exception) {
    
} catch (ExceptionB exception) {
    
} catch (ExceptionC exception) {
    
}

It’s a chain of if statements in disguise.

if (throwable instanceof ExceptionA) {
    ExceptionA exception = (ExceptionA) throwable;
    
} elseif (throwable instanceof ExceptionB) {
    ExceptionB exception = (ExceptionB) throwable;
    
} elseif (throwable instanceof ExceptionC) {
    ExceptionC exception = (ExceptionC) throwable;
    
}

It is very much the kind of code that is disliked by OOP purists. It’s the way we implement polymorphism, when we don’t have control over the implementation of the callee [i].

Usually, polymorphism is achieved by defining functions on the callee. With exception handling, the handling needs to be defined by the caller, because each caller needs a different handling process.


[i] Google “External Polymorphism” for similar concepts.

Contents

Background

Review of Exception Handling Basics

The Requirements

The Three-tiered Exception Handling Architecture

Summary