2
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ @Sean I'm unsure about this question. It could be about the general terminology in programming languages, which is definitely on topic here. If, instead, it's about Python-specific terminology, then I agree with you -- it is off-topic here. $\endgroup$
    – chi
    Jun 5, 2018 at 13:02
  • 2
    $\begingroup$ @chi Every language know that uses exceptions "throws" them, so this is essentially language-independent. $\endgroup$ Jun 5, 2018 at 14:35
  • $\begingroup$ @DavidRicherby Agreed. I also agree with the re-tagging. For what it is worth: python, ruby, ada, delphi and ocaml use a raise syntax to throw exceptions. en.wikipedia.org/wiki/Exception_handling_syntax $\endgroup$
    – chi
    Jun 5, 2018 at 15:05
  • $\begingroup$ See also a similar question on English Language & Usage for a linguistic perspective. $\endgroup$ Jun 6, 2018 at 0:05
  • $\begingroup$ Thank you all so much! I found the linguistic perspective very interesting as well @Gilles $\endgroup$
    – Anna
    Jun 7, 2018 at 6:28

1 Answer 1

6
$\begingroup$

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) evaluates x and returns its values, except that if during the evaluation of x (throw y) should be evaluated, catch immediately returns y without further evaluating x.

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 with catch as a structured nonlocal exit mechanism.

(throw x) evaluates x and throws the value back to the most recent catch.

(throw x <tag>) throws the value of x back to the most recent catch 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).

$\endgroup$
2
  • $\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
    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$
    – mseddon
    Mar 31 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.