I'm just wondering where the phrase "throwing" an exception came from. I understand "raising" an exception but I'm wondering if "throwing" has a reason or meaning behind it?
1 Answer
As far as I know, and Wikipedia confirms, exceptions in the software sense originated in Lisp. There's a good summary in A Pattern of Language Evolution by Gabriel and Steele. Exceptions (which were not yet called exceptions) arose from the need to specify the behavior of a program if an error occurs. One possibility is to halt the program, but this is not always helpful. Lisp implementations traditionally have had a way to enter the debugger on an error, but sometimes programmers wanted to include error handling in their program. So 1960s Lisp implementations had a way to say “do this, and if an error occurs then do that instead”. Originally errors came from primitive functions, but programmers found it convenient to deliberately trigger an error in order to skip some part of the program and jump to the error handler.
In 1972, the modern form of exception handling in Lisp appeared in MacLisp: throw
and catch
. The Software Preservation Group lists a lot of material on early Lisp implementations, including The MACLISP Reference Manual Revision 0 by David Moon. The primitives catch
and throw
are documented in §5.3 p.43.
catch
is the LISP function for doing structured non-local exits.(catch x)
evaluatesx
and returns its values, except that if during the evaluation ofx
(throw y)
should be evaluated,catch
immediately returnsy
without further evaluatingx
.
catch
may also be used with a econd argument, not evaluated, which is used as a tag to distinguish between nested catches. (…)
throw
is used withcatch
as a structured nonlocal exit mechanism.
(throw x)
evaluatesx
and throws the value back to the most recentcatch
.
(throw x <tag>)
throws the value ofx
back to the most recentcatch
labelled with<tag>
or unlabelled.
The focus is on nonlocal control flow. It's a form of goto (an upward-only goto), which is also called a jump. The metaphor is that one part of the program throws the value to return to the exception handler, and the exception handler catches that value and returns it.
I don't know when software people started using “raise” for exceptions, or for that matter when they started calling them “exceptions”. I think both terms originated from hardware, but I don't know for sure. They were well-established by the mid-1980s, for example it's the terminology used in ML (see Monahan's thesis Data Type Proofs using Edinburgh LCF, 1984) and in Ada (defined in 1983).
-
$\begingroup$ The important point here is that what makes something an exception is that it is caught. If you don't catch an exception, it may as well be an assertion. $\endgroup$– Pseudonym ♦Commented Jun 6, 2018 at 1:34
-
$\begingroup$ One of the most ironic historical outcomes of this was that throw/catch were introduced to prevent people abusing error handling for control flow. Today, we abuse Landin's J operator for error handling. $\endgroup$– mseddonCommented Mar 31, 2022 at 16:11
raise
syntax to throw exceptions. en.wikipedia.org/wiki/Exception_handling_syntax $\endgroup$