Exceptions require the following features:
- Have an arbitrary number of associated values/objects (describing the details of the exception).
- Be organized hierarchically, via inheritance or object conversions.
- Support a default action if uncaught.
Issue 1
An exception thrown in the middle of a procedure that isn't handle can cause constraints to be violated since the procedure didn't finish. Resources may be acquired but never released.
Maybe exceptions shouldn't propagate past the immediate caller. Exceptions were created to decouple error-handling from the regular code, but allowing exceptions to propagate past the caller exposes implementation details.
Is Java's solution sufficient? In Java, unhandled exceptions must be declared and so are part of a method's published interface.
Issue 2
Exceptions as objects support the inheritance requirement well but fail in other areas. Passing multiple values is a tedious procedure require constructors and member variables. I see exceptions more like functions accepting parameters than classes. A function-like syntax would be immensely convenient.
throw
divideByZero(numerator, denominator).
At first look name overloading seems too complex and requires too much syntax to be useful.
class Array:
procedure find:
throws: itemNotFound.
exception itemNotFound(key
is Integer):
print("Item not found with key [key].").
...
print("The first item in the array is [find(array, 0)].").
catch Array.itemNotFound(key):
print("Item [key] not
found.").