So why is Throwable a class?
I can think of two reasons:
- Exceptions have state. In particular, message, cause, and stack trace.
- It is easier for the JVM to implement efficient catch blocks. Class hierarchy checks are cheaper than interface checks.
Wouldn't exception handling be easier
if Throwable were an interface?
Exception handling is a hard topic regardless of whether exceptions are classes or interfaces. I actually suspect it would make it harder on Java programmers if they have to order their catch blocks based on arbitrary interfaces rather than on class hierarchies.
But could it be made abstract?
In theory, yes. In practice, no. Too much code depends on being able to create an instance of Throwable in order to call getStackTrace.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…