ArticleslgStudy

computer science

Exception handling (programming)

Exception handling (programming) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Exception handling (programming) rather than just read about it. In short: In computer programming, several programming language mechanisms exist for exception handling. The term exception is typically used to denote a data structure storing information about an exceptional condition.

Key takeaways

  • Exception handling (programming) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Exception handling (programming) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Exception handling (programming) from memory before moving on to harder problems.

Reference excerpt

In computer programming, several programming language mechanisms exist for exception handling. The term exception is typically used to denote a data structure storing information about an exceptional condition. One mechanism to transfer control, or raise an exception, is known as a throw; the exception is said to be thrown. Execution is transferred to a catch.

Usage Programming languages differ substantially in their notion of what an exception is. Exceptions can be used to represent and handle abnormal, unpredictable, erroneous situations, but also as flow control structures to handle normal situations. For example, Python's iterators throw StopIteration exceptions to signal that there are no further items produced by the iterator. There is disagreement within many languages as to what constitutes idiomatic usage of exceptions. For example, Joshua Bloch states that Java's exceptions should only be used for exceptional situations, but Kiniry observes that Java's java.io.FileNotFoundException class is not at all an exceptional event. Similarly, Bjarne Stroustrup, author of C++, states that C++ exceptions should only be used for error handling, as this is what they were designed for, but Kiniry observes that many modern languages such as Ada, C++, Modula-3, ML and OCaml, Python, and Ruby use exceptions for flow control. Some languages such as Eiffel, C#, Common Lisp, and Modula-2 have made a concerted effort to restrict their usage of exceptions, although this is done on a social rather than technical level.

History The earliest IBM Fortran compilers had statements for testing exceptional conditions. These included the IF ACCUMULATOR OVERFLOW, IF QUOTIENT OVERFLOW, and IF DIVIDE CHECK statements. In the interest of machine independence, they were not included in FORTRAN IV nor the Fortran 66 Standard. However since Fortran 2003 it is possible to test for numerical issues via calls to functions in the IEEE_EXCEPTIONS module. Software exception handling continued to be developed in the 1960s and 1970s. LISP 1.5 (1961) allowed exceptions to be raised by the ERROR pseudo-function, similarly to errors raised by the interpreter or compiler. Exceptions were caught by the ERRORSET keyword, which returned NIL in case of an error, instead of terminating the program or entering the debugger. PL/I introduced its own form of exception handling circa 1964, allowing interrupts to be handled with ON units. MacLisp observed that ERRSET and ERR were used not only for error raising, but for non-local control flow, and thus added two new keywords, CATCH and THROW (June 1972). The cleanup behavior now generally called "finally" was introduced in NIL (New Implementation of LISP) in the mid- to late-1970s as UNWIND-PROTECT. This was then adopted by Common Lisp. Contemporary with this was dynamic-wind in Scheme, which handled exceptions in closures. The first papers on structured exception handling were Goodenough (1975a) and Goodenough (1975b). Exception handling was subsequently widely adopted by many programming languages from the 1980s onward.

Syntax

Many computer languages have built-in syntactic support for exceptions and exception handling. This includes Ada, BlitzMax, C++, C#, Clojure, COBOL, D, ECMAScript (e.g., ActionScript, JavaScript), Eiffel, Java, ML, Object Pascal (e.g., Delphi, Free Pascal), PowerBuilder, Objective-C, OCaml, Perl, PHP (as of version 5), PL/I, PL/SQL, Prolog, Python, REALbasic, Ruby, Scala, Smalltalk, Tcl, Visual Prolog, and most .NET languages. Excluding minor syntactic differences, only a few exception handling styles are in use. In the most popular style, an exception is initiated by a special statement (throw or raise) with an exception object (e.g., with Java or Object Pascal) or a value of a special extendable enumerated type (e.g., with Ada or SML). The scope for exception handlers starts with a marker clause (try or the language's block starter such as begin) and ends in the start of the first handler clause (catch, except, rescue). Several handler clauses can follow, and each can specify which exception types it handles and what name it uses for the exception object. As a minor variation, some languages use a single handler clause, which deals with the class of the exception internally. Also common is a related clause (finally or ensure) that is executed whether an exception occurred or not, typically to release resources acquired within the body of the exception-handling block. Notably, C++ does not provide this construct, recommending instead the resource acquisition is initialization (RAII) technique which frees resources using destructors. According to a 2008 paper by Westley Weimer and George Necula, the syntax of the try...finally blocks in Java is a contributing factor to software defects. When a method needs to handle the acquisition and release of 3–5 resources, programmers are apparently unwilling to nest enough blocks due to readability concerns, even when this would be a correct solution. It is possible to use a single try...finally block even when dealing with multiple resources, but that requires a correct use of sentinel values, which is another common source of bugs for this type of problem. Python and Ruby also permit a clause (else) that is used in case no exception occurred before the end of the handler's scope was reached. In its whole, exception handling code might look like this (in Java-style exception-handling syntax):

C does not have try-catch exception handling, but uses return codes for error checking. The setjmp and longjmp standard library functions can be used to implement try-catch handling via macros. Perl 5 uses die for throw and eval {} if ($@) {} for try-catch. It has CPAN modules that offer try-catch semantics.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Exception handling (programming)

Start with the simplest possible case. Write down what Exception handling (programming) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Exception handling (programming) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Exception handling (programming) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Exception handling (programming)

In research
Exception handling (programming) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Exception handling (programming) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Exception handling (programming) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Control flow, Software anomalies, so understanding it makes those chapters shorter.
In everyday life
Look for Exception handling (programming) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Exception handling (programming)” →

How to study Exception handling (programming) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Exception handling (programming) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Exception handling (programming) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Exception handling (programming) in simple terms?

In computer programming, several programming language mechanisms exist for exception handling. The term exception is typically used to denote a data structure storing information about an exceptional condition.

Why does Exception handling (programming) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Exception handling (programming)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Exception handling (programming).

Tags

  • Control flow
  • Software anomalies

Keep exploring