<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Control.OldException</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ window.onload = function () {pageLoad();setSynopsis("mini_Control-OldException.html");}; //]]> </script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">base-4.3.1.0: Basic libraries</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>non-portable (extended exceptions)</td></tr><tr><th>Stability</th><td>experimental</td></tr><tr><th>Maintainer</th><td>libraries@haskell.org</td></tr></table><p class="caption">Control.OldException</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">The Exception type </a></li><li><a href="#g:2">Throwing exceptions </a></li><li><a href="#g:3">Catching Exceptions </a><ul><li><a href="#g:4">The <code>catch</code> functions </a></li><li><a href="#g:5">The <code>handle</code> functions </a></li><li><a href="#g:6">The <code>try</code> functions </a></li><li><a href="#g:7">The <code>evaluate</code> function </a></li><li><a href="#g:8">The <code>mapException</code> function </a></li><li><a href="#g:9">Exception predicates </a></li></ul></li><li><a href="#g:10">Dynamic exceptions </a></li><li><a href="#g:11">Asynchronous Exceptions </a><ul><li><a href="#g:12">Asynchronous exception control </a><ul><li><a href="#g:13">Applying <code>block</code> to an exception handler </a></li><li><a href="#g:14">Interruptible operations </a></li></ul></li></ul></li><li><a href="#g:15">Assertions </a></li><li><a href="#g:16">Utilities </a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><p>This module provides support for raising and catching both built-in and user-defined exceptions. </p><p>In addition to exceptions thrown by <code><a href="System-IO.html#t:IO">IO</a></code> operations, exceptions may be thrown by pure code (imprecise exceptions) or by external events (asynchronous exceptions), but may only be caught in the <code><a href="System-IO.html#t:IO">IO</a></code> monad. For more details, see: </p><ul><li> <em>A semantics for imprecise exceptions</em>, by Simon Peyton Jones, Alastair Reid, Tony Hoare, Simon Marlow, Fergus Henderson, in <em>PLDI'99</em>. </li><li> <em>Asynchronous exceptions in Haskell</em>, by Simon Marlow, Simon Peyton Jones, Andy Moran and John Reppy, in <em>PLDI'01</em>. </li></ul></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">data</span> <a href="#t:Exception">Exception</a> <ul class="subs"><li>= <a href="#v:ArithException">ArithException</a> <a href="Control-OldException.html#t:ArithException">ArithException</a> </li><li>| <a href="#v:ArrayException">ArrayException</a> <a href="Control-OldException.html#t:ArrayException">ArrayException</a> </li><li>| <a href="#v:AssertionFailed">AssertionFailed</a> <a href="Data-Char.html#t:String">String</a> </li><li>| <a href="#v:AsyncException">AsyncException</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a> </li><li>| <a href="#v:BlockedOnDeadMVar">BlockedOnDeadMVar</a> </li><li>| <a href="#v:BlockedIndefinitely">BlockedIndefinitely</a> </li><li>| <a href="#v:NestedAtomically">NestedAtomically</a> </li><li>| <a href="#v:Deadlock">Deadlock</a> </li><li>| <a href="#v:DynException">DynException</a> <a href="Data-Dynamic.html#t:Dynamic">Dynamic</a> </li><li>| <a href="#v:ErrorCall">ErrorCall</a> <a href="Data-Char.html#t:String">String</a> </li><li>| <a href="#v:ExitException">ExitException</a> <a href="System-Exit.html#t:ExitCode">ExitCode</a> </li><li>| <a href="#v:IOException">IOException</a> <a href="Control-OldException.html#t:IOException">IOException</a> </li><li>| <a href="#v:NoMethodError">NoMethodError</a> <a href="Data-Char.html#t:String">String</a> </li><li>| <a href="#v:NonTermination">NonTermination</a> </li><li>| <a href="#v:PatternMatchFail">PatternMatchFail</a> <a href="Data-Char.html#t:String">String</a> </li><li>| <a href="#v:RecConError">RecConError</a> <a href="Data-Char.html#t:String">String</a> </li><li>| <a href="#v:RecSelError">RecSelError</a> <a href="Data-Char.html#t:String">String</a> </li><li>| <a href="#v:RecUpdError">RecUpdError</a> <a href="Data-Char.html#t:String">String</a> </li></ul></li><li class="src short"><span class="keyword">data</span> <a href="#t:IOException">IOException</a> </li><li class="src short"><span class="keyword">data</span> <a href="#t:ArithException">ArithException</a> <ul class="subs"><li>= <a href="#v:Overflow">Overflow</a> </li><li>| <a href="#v:Underflow">Underflow</a> </li><li>| <a href="#v:LossOfPrecision">LossOfPrecision</a> </li><li>| <a href="#v:DivideByZero">DivideByZero</a> </li><li>| <a href="#v:Denormal">Denormal</a> </li></ul></li><li class="src short"><span class="keyword">data</span> <a href="#t:ArrayException">ArrayException</a> <ul class="subs"><li>= <a href="#v:IndexOutOfBounds">IndexOutOfBounds</a> <a href="Data-Char.html#t:String">String</a> </li><li>| <a href="#v:UndefinedElement">UndefinedElement</a> <a href="Data-Char.html#t:String">String</a> </li></ul></li><li class="src short"><span class="keyword">data</span> <a href="#t:AsyncException">AsyncException</a> <ul class="subs"><li>= <a href="#v:StackOverflow">StackOverflow</a> </li><li>| <a href="#v:HeapOverflow">HeapOverflow</a> </li><li>| <a href="#v:ThreadKilled">ThreadKilled</a> </li><li>| <a href="#v:UserInterrupt">UserInterrupt</a> </li></ul></li><li class="src short"><a href="#v:throwIO">throwIO</a> :: <a href="Control-Exception-Base.html#t:Exception">Exception</a> e => e -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:throw">throw</a> :: <a href="Control-Exception-Base.html#t:Exception">Exception</a> e => e -> a</li><li class="src short"><a href="#v:ioError">ioError</a> :: <a href="System-IO-Error.html#t:IOError">IOError</a> -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:throwTo">throwTo</a> :: <a href="Control-Exception-Base.html#t:Exception">Exception</a> e => <a href="Control-Concurrent.html#t:ThreadId">ThreadId</a> -> e -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></li><li class="src short"><a href="#v:catch">catch</a> :: <a href="System-IO.html#t:IO">IO</a> a -> (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:catchJust">catchJust</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> b) -> <a href="System-IO.html#t:IO">IO</a> a -> (b -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:handle">handle</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:handleJust">handleJust</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> b) -> (b -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:try">try</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> (<a href="Data-Either.html#t:Either">Either</a> <a href="Control-OldException.html#t:Exception">Exception</a> a)</li><li class="src short"><a href="#v:tryJust">tryJust</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> b) -> <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> (<a href="Data-Either.html#t:Either">Either</a> b a)</li><li class="src short"><a href="#v:evaluate">evaluate</a> :: a -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:mapException">mapException</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Control-OldException.html#t:Exception">Exception</a>) -> a -> a</li><li class="src short"><a href="#v:ioErrors">ioErrors</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="System-IO-Error.html#t:IOError">IOError</a></li><li class="src short"><a href="#v:arithExceptions">arithExceptions</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Control-OldException.html#t:ArithException">ArithException</a></li><li class="src short"><a href="#v:errorCalls">errorCalls</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Data-Char.html#t:String">String</a></li><li class="src short"><a href="#v:dynExceptions">dynExceptions</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Data-Dynamic.html#t:Dynamic">Dynamic</a></li><li class="src short"><a href="#v:assertions">assertions</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Data-Char.html#t:String">String</a></li><li class="src short"><a href="#v:asyncExceptions">asyncExceptions</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a></li><li class="src short"><a href="#v:userErrors">userErrors</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Data-Char.html#t:String">String</a></li><li class="src short"><a href="#v:throwDyn">throwDyn</a> :: <a href="Data-Typeable.html#t:Typeable">Typeable</a> exception => exception -> b</li><li class="src short"><a href="#v:throwDynTo">throwDynTo</a> :: <a href="Data-Typeable.html#t:Typeable">Typeable</a> exception => <a href="Control-Concurrent.html#t:ThreadId">ThreadId</a> -> exception -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></li><li class="src short"><a href="#v:catchDyn">catchDyn</a> :: <a href="Data-Typeable.html#t:Typeable">Typeable</a> exception => <a href="System-IO.html#t:IO">IO</a> a -> (exception -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:block">block</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:unblock">unblock</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:assert">assert</a> :: <a href="Data-Bool.html#t:Bool">Bool</a> -> a -> a</li><li class="src short"><a href="#v:bracket">bracket</a> :: <a href="System-IO.html#t:IO">IO</a> a -> (a -> <a href="System-IO.html#t:IO">IO</a> b) -> (a -> <a href="System-IO.html#t:IO">IO</a> c) -> <a href="System-IO.html#t:IO">IO</a> c</li><li class="src short"><a href="#v:bracket_">bracket_</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> b -> <a href="System-IO.html#t:IO">IO</a> c -> <a href="System-IO.html#t:IO">IO</a> c</li><li class="src short"><a href="#v:bracketOnError">bracketOnError</a> :: <a href="System-IO.html#t:IO">IO</a> a -> (a -> <a href="System-IO.html#t:IO">IO</a> b) -> (a -> <a href="System-IO.html#t:IO">IO</a> c) -> <a href="System-IO.html#t:IO">IO</a> c</li><li class="src short"><a href="#v:finally">finally</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> b -> <a href="System-IO.html#t:IO">IO</a> a</li><li class="src short"><a href="#v:setUncaughtExceptionHandler">setUncaughtExceptionHandler</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a>) -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></li><li class="src short"><a href="#v:getUncaughtExceptionHandler">getUncaughtExceptionHandler</a> :: <a href="System-IO.html#t:IO">IO</a> (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a>)</li></ul></div><div id="interface"><h1 id="g:1">The Exception type </h1><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:Exception" class="def">Exception</a> </p><div class="doc"><p>The type of exceptions. Every kind of system-generated exception has a constructor in the <code><a href="Control-OldException.html#t:Exception">Exception</a></code> type, and values of other types may be injected into <code><a href="Control-OldException.html#t:Exception">Exception</a></code> by coercing them to <code><a href="Data-Dynamic.html#t:Dynamic">Dynamic</a></code> (see the section on Dynamic Exceptions: <a href="Control-OldException.html#DynamicExceptions">Control.OldException</a>). </p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:ArithException" class="def">ArithException</a> <a href="Control-OldException.html#t:ArithException">ArithException</a></td><td class="doc"><p>Exceptions raised by arithmetic operations. (NOTE: GHC currently does not throw <code><a href="Control-OldException.html#v:ArithException">ArithException</a></code>s except for <code>DivideByZero</code>). </p></td></tr><tr><td class="src"><a name="v:ArrayException" class="def">ArrayException</a> <a href="Control-OldException.html#t:ArrayException">ArrayException</a></td><td class="doc"><p>Exceptions raised by array-related operations. (NOTE: GHC currently does not throw <code><a href="Control-OldException.html#v:ArrayException">ArrayException</a></code>s). </p></td></tr><tr><td class="src"><a name="v:AssertionFailed" class="def">AssertionFailed</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>This exception is thrown by the <code><a href="Control-OldException.html#v:assert">assert</a></code> operation when the condition fails. The <code><a href="Data-Char.html#t:String">String</a></code> argument contains the location of the assertion in the source program. </p></td></tr><tr><td class="src"><a name="v:AsyncException" class="def">AsyncException</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a></td><td class="doc"><p>Asynchronous exceptions (see section on Asynchronous Exceptions: <a href="Control-OldException.html#AsynchronousExceptions">Control.OldException</a>). </p></td></tr><tr><td class="src"><a name="v:BlockedOnDeadMVar" class="def">BlockedOnDeadMVar</a></td><td class="doc"><p>The current thread was executing a call to <code>Control.Concurrent.MVar.takeMVar</code> that could never return, because there are no other references to this <code>MVar</code>. </p></td></tr><tr><td class="src"><a name="v:BlockedIndefinitely" class="def">BlockedIndefinitely</a></td><td class="doc"><p>The current thread was waiting to retry an atomic memory transaction that could never become possible to complete because there are no other threads referring to any of the TVars involved. </p></td></tr><tr><td class="src"><a name="v:NestedAtomically" class="def">NestedAtomically</a></td><td class="doc"><p>The runtime detected an attempt to nest one STM transaction inside another one, presumably due to the use of <code>unsafePeformIO</code> with <code><a href="GHC-Conc.html#v:atomically">atomically</a></code>. </p></td></tr><tr><td class="src"><a name="v:Deadlock" class="def">Deadlock</a></td><td class="doc"><p>There are no runnable threads, so the program is deadlocked. The <code><a href="Control-OldException.html#v:Deadlock">Deadlock</a></code> exception is raised in the main thread only (see also: <a href="Control-Concurrent.html">Control.Concurrent</a>). </p></td></tr><tr><td class="src"><a name="v:DynException" class="def">DynException</a> <a href="Data-Dynamic.html#t:Dynamic">Dynamic</a></td><td class="doc"><p>Dynamically typed exceptions (see section on Dynamic Exceptions: <a href="Control-OldException.html#DynamicExceptions">Control.OldException</a>). </p></td></tr><tr><td class="src"><a name="v:ErrorCall" class="def">ErrorCall</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>The <code><a href="Control-OldException.html#v:ErrorCall">ErrorCall</a></code> exception is thrown by <code><a href="Prelude.html#v:error">error</a></code>. The <code><a href="Data-Char.html#t:String">String</a></code> argument of <code><a href="Control-OldException.html#v:ErrorCall">ErrorCall</a></code> is the string passed to <code><a href="Prelude.html#v:error">error</a></code> when it was called. </p></td></tr><tr><td class="src"><a name="v:ExitException" class="def">ExitException</a> <a href="System-Exit.html#t:ExitCode">ExitCode</a></td><td class="doc"><p>The <code><a href="Control-OldException.html#v:ExitException">ExitException</a></code> exception is thrown by <code>System.Exit.exitWith</code> (and <code>System.Exit.exitFailure</code>). The <code>ExitCode</code> argument is the value passed to <code>System.Exit.exitWith</code>. An unhandled <code><a href="Control-OldException.html#v:ExitException">ExitException</a></code> exception in the main thread will cause the program to be terminated with the given exit code. </p></td></tr><tr><td class="src"><a name="v:IOException" class="def">IOException</a> <a href="Control-OldException.html#t:IOException">IOException</a></td><td class="doc"><p>These are the standard IO exceptions generated by Haskell's <code>IO</code> operations. See also <a href="System-IO-Error.html">System.IO.Error</a>. </p></td></tr><tr><td class="src"><a name="v:NoMethodError" class="def">NoMethodError</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>An attempt was made to invoke a class method which has no definition in this instance, and there was no default definition given in the class declaration. GHC issues a warning when you compile an instance which has missing methods. </p></td></tr><tr><td class="src"><a name="v:NonTermination" class="def">NonTermination</a></td><td class="doc"><p>The current thread is stuck in an infinite loop. This exception may or may not be thrown when the program is non-terminating. </p></td></tr><tr><td class="src"><a name="v:PatternMatchFail" class="def">PatternMatchFail</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>A pattern matching failure. The <code><a href="Data-Char.html#t:String">String</a></code> argument should contain a descriptive message including the function name, source file and line number. </p></td></tr><tr><td class="src"><a name="v:RecConError" class="def">RecConError</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>An attempt was made to evaluate a field of a record for which no value was given at construction time. The <code><a href="Data-Char.html#t:String">String</a></code> argument gives the location of the record construction in the source program. </p></td></tr><tr><td class="src"><a name="v:RecSelError" class="def">RecSelError</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>A field selection was attempted on a constructor that doesn't have the requested field. This can happen with multi-constructor records when one or more fields are missing from some of the constructors. The <code><a href="Data-Char.html#t:String">String</a></code> argument gives the location of the record selection in the source program. </p></td></tr><tr><td class="src"><a name="v:RecUpdError" class="def">RecUpdError</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>An attempt was made to update a field in a record, where the record doesn't have the requested field. This can only occur with multi-constructor records, when one or more fields are missing from some of the constructors. The <code><a href="Data-Char.html#t:String">String</a></code> argument gives the location of the record update in the source program. </p></td></tr></table></div><div class="subs instances"><p id="control.i:Exception" class="caption collapser" onclick="toggleSection('i:Exception')">Instances</p><div id="section.i:Exception" class="show"><table><tr><td class="src"><a href="Data-Eq.html#t:Eq">Eq</a> <a href="Control-OldException.html#t:Exception">Exception</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Text-Show.html#t:Show">Show</a> <a href="Control-OldException.html#t:Exception">Exception</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Typeable.html#t:Typeable">Typeable</a> <a href="Control-OldException.html#t:Exception">Exception</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Exception-Base.html#t:Exception">Exception</a> <a href="Control-OldException.html#t:Exception">Exception</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:IOException" class="def">IOException</a> </p><div class="doc"><p>Exceptions that occur in the <code>IO</code> monad. An <code>IOException</code> records a more specific error type, a descriptive string and maybe the handle that was used when the error was flagged. </p></div><div class="subs instances"><p id="control.i:IOException" class="caption collapser" onclick="toggleSection('i:IOException')">Instances</p><div id="section.i:IOException" class="show"><table><tr><td class="src"><a href="Data-Eq.html#t:Eq">Eq</a> <a href="Control-OldException.html#t:IOException">IOException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Text-Show.html#t:Show">Show</a> <a href="Control-OldException.html#t:IOException">IOException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Typeable.html#t:Typeable">Typeable</a> <a href="Control-OldException.html#t:IOException">IOException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Exception-Base.html#t:Exception">Exception</a> <a href="Control-OldException.html#t:IOException">IOException</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:ArithException" class="def">ArithException</a> </p><div class="doc"><p>Arithmetic exceptions. </p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:Overflow" class="def">Overflow</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a name="v:Underflow" class="def">Underflow</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a name="v:LossOfPrecision" class="def">LossOfPrecision</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a name="v:DivideByZero" class="def">DivideByZero</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a name="v:Denormal" class="def">Denormal</a></td><td class="doc empty"> </td></tr></table></div><div class="subs instances"><p id="control.i:ArithException" class="caption collapser" onclick="toggleSection('i:ArithException')">Instances</p><div id="section.i:ArithException" class="show"><table><tr><td class="src"><a href="Data-Eq.html#t:Eq">Eq</a> <a href="Control-OldException.html#t:ArithException">ArithException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Ord.html#t:Ord">Ord</a> <a href="Control-OldException.html#t:ArithException">ArithException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Text-Show.html#t:Show">Show</a> <a href="Control-OldException.html#t:ArithException">ArithException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Typeable.html#t:Typeable">Typeable</a> <a href="Control-OldException.html#t:ArithException">ArithException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Exception-Base.html#t:Exception">Exception</a> <a href="Control-OldException.html#t:ArithException">ArithException</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:ArrayException" class="def">ArrayException</a> </p><div class="doc"><p>Exceptions generated by array operations </p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:IndexOutOfBounds" class="def">IndexOutOfBounds</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>An attempt was made to index an array outside its declared bounds. </p></td></tr><tr><td class="src"><a name="v:UndefinedElement" class="def">UndefinedElement</a> <a href="Data-Char.html#t:String">String</a></td><td class="doc"><p>An attempt was made to evaluate an element of an array that had not been initialized. </p></td></tr></table></div><div class="subs instances"><p id="control.i:ArrayException" class="caption collapser" onclick="toggleSection('i:ArrayException')">Instances</p><div id="section.i:ArrayException" class="show"><table><tr><td class="src"><a href="Data-Eq.html#t:Eq">Eq</a> <a href="Control-OldException.html#t:ArrayException">ArrayException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Ord.html#t:Ord">Ord</a> <a href="Control-OldException.html#t:ArrayException">ArrayException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Text-Show.html#t:Show">Show</a> <a href="Control-OldException.html#t:ArrayException">ArrayException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Typeable.html#t:Typeable">Typeable</a> <a href="Control-OldException.html#t:ArrayException">ArrayException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Exception-Base.html#t:Exception">Exception</a> <a href="Control-OldException.html#t:ArrayException">ArrayException</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><span class="keyword">data</span> <a name="t:AsyncException" class="def">AsyncException</a> </p><div class="doc"><p>Asynchronous exceptions. </p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:StackOverflow" class="def">StackOverflow</a></td><td class="doc"><p>The current thread's stack exceeded its limit. Since an exception has been raised, the thread's stack will certainly be below its limit again, but the programmer should take remedial action immediately. </p></td></tr><tr><td class="src"><a name="v:HeapOverflow" class="def">HeapOverflow</a></td><td class="doc"><p>The program's heap is reaching its limit, and the program should take action to reduce the amount of live data it has. Notes: </p><ul><li> It is undefined which thread receives this exception. </li><li> GHC currently does not throw <code><a href="Control-OldException.html#v:HeapOverflow">HeapOverflow</a></code> exceptions. </li></ul></td></tr><tr><td class="src"><a name="v:ThreadKilled" class="def">ThreadKilled</a></td><td class="doc"><p>This exception is raised by another thread calling <code>Control.Concurrent.killThread</code>, or by the system if it needs to terminate the thread for some reason. </p></td></tr><tr><td class="src"><a name="v:UserInterrupt" class="def">UserInterrupt</a></td><td class="doc"><p>This exception is raised by default in the main thread of the program when the user requests to terminate the program via the usual mechanism(s) (e.g. Control-C in the console). </p></td></tr></table></div><div class="subs instances"><p id="control.i:AsyncException" class="caption collapser" onclick="toggleSection('i:AsyncException')">Instances</p><div id="section.i:AsyncException" class="show"><table><tr><td class="src"><a href="Data-Eq.html#t:Eq">Eq</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Ord.html#t:Ord">Ord</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Text-Show.html#t:Show">Show</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Data-Typeable.html#t:Typeable">Typeable</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a></td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Exception-Base.html#t:Exception">Exception</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a></td><td class="doc empty"> </td></tr></table></div></div></div><h1 id="g:2">Throwing exceptions </h1><div class="top"><p class="src"><a name="v:throwIO" class="def">throwIO</a> :: <a href="Control-Exception-Base.html#t:Exception">Exception</a> e => e -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>A variant of <code><a href="Control-OldException.html#v:throw">throw</a></code> that can only be used within the <code><a href="System-IO.html#t:IO">IO</a></code> monad. </p><p>Although <code><a href="Control-OldException.html#v:throwIO">throwIO</a></code> has a type that is an instance of the type of <code><a href="Control-OldException.html#v:throw">throw</a></code>, the two functions are subtly different: </p><pre> throw e `seq` x ===> throw e throwIO e `seq` x ===> x </pre><p>The first example will cause the exception <code>e</code> to be raised, whereas the second one won't. In fact, <code><a href="Control-OldException.html#v:throwIO">throwIO</a></code> will only cause an exception to be raised when it is used within the <code><a href="System-IO.html#t:IO">IO</a></code> monad. The <code><a href="Control-OldException.html#v:throwIO">throwIO</a></code> variant should be used in preference to <code><a href="Control-OldException.html#v:throw">throw</a></code> to raise an exception within the <code><a href="System-IO.html#t:IO">IO</a></code> monad because it guarantees ordering with respect to other <code><a href="System-IO.html#t:IO">IO</a></code> operations, whereas <code><a href="Control-OldException.html#v:throw">throw</a></code> does not. </p></div></div><div class="top"><p class="src"><a name="v:throw" class="def">throw</a> :: <a href="Control-Exception-Base.html#t:Exception">Exception</a> e => e -> a</p><div class="doc"><p>Throw an exception. Exceptions may be thrown from purely functional code, but may only be caught within the <code><a href="System-IO.html#t:IO">IO</a></code> monad. </p></div></div><div class="top"><p class="src"><a name="v:ioError" class="def">ioError</a> :: <a href="System-IO-Error.html#t:IOError">IOError</a> -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>Raise an <code><a href="System-IO-Error.html#t:IOError">IOError</a></code> in the <code><a href="System-IO.html#t:IO">IO</a></code> monad. </p></div></div><div class="top"><p class="src"><a name="v:throwTo" class="def">throwTo</a> :: <a href="Control-Exception-Base.html#t:Exception">Exception</a> e => <a href="Control-Concurrent.html#t:ThreadId">ThreadId</a> -> e -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></p><div class="doc"><p><code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> raises an arbitrary exception in the target thread (GHC only). </p><p><code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> does not return until the exception has been raised in the target thread. The calling thread can thus be certain that the target thread has received the exception. This is a useful property to know when dealing with race conditions: eg. if there are two threads that can kill each other, it is guaranteed that only one of the threads will get to kill the other. </p><p>Whatever work the target thread was doing when the exception was raised is not lost: the computation is suspended until required by another thread. </p><p>If the target thread is currently making a foreign call, then the exception will not be raised (and hence <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> will not return) until the call has completed. This is the case regardless of whether the call is inside a <code><a href="Control-Exception-Base.html#v:mask">mask</a></code> or not. </p><p>Important note: the behaviour of <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> differs from that described in the paper "Asynchronous exceptions in Haskell" (<a href="http://research.microsoft.com/~simonpj/Papers/asynch-exns.htm">http://research.microsoft.com/~simonpj/Papers/asynch-exns.htm</a>). In the paper, <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> is non-blocking; but the library implementation adopts a more synchronous design in which <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> does not return until the exception is received by the target thread. The trade-off is discussed in Section 9 of the paper. Like any blocking operation, <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> is therefore interruptible (see Section 5.3 of the paper). Unlike other interruptible operations, however, <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> is <em>always</em> interruptible, even if it does not actually block. </p><p>There is no guarantee that the exception will be delivered promptly, although the runtime will endeavour to ensure that arbitrary delays don't occur. In GHC, an exception can only be raised when a thread reaches a <em>safe point</em>, where a safe point is where memory allocation occurs. Some loops do not perform any memory allocation inside the loop and therefore cannot be interrupted by a <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code>. </p><p>Blocked <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> is fair: if multiple threads are trying to throw an exception to the same target thread, they will succeed in FIFO order. </p></div></div><h1 id="g:3">Catching Exceptions </h1><div class="doc"><p>There are several functions for catching and examining exceptions; all of them may only be used from within the <code><a href="System-IO.html#t:IO">IO</a></code> monad. </p></div><h2 id="g:4">The <code>catch</code> functions </h2><div class="top"><p class="src"><a name="v:catch" class="def">catch</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="System-IO.html#t:IO">IO</a> a</td><td class="doc"><p>The computation to run </p></td></tr><tr><td class="src">-> (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="System-IO.html#t:IO">IO</a> a)</td><td class="doc"><p>Handler to invoke if an exception is raised </p></td></tr><tr><td class="src">-> <a href="System-IO.html#t:IO">IO</a> a</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>This is the simplest of the exception-catching functions. It takes a single argument, runs it, and if an exception is raised the "handler" is executed, with the value of the exception passed as an argument. Otherwise, the result is returned as normal. For example: </p><pre> catch (openFile f ReadMode) (\e -> hPutStr stderr ("Couldn't open "++f++": " ++ show e)) </pre><p>For catching exceptions in pure (non-<code><a href="System-IO.html#t:IO">IO</a></code>) expressions, see the function <code><a href="Control-OldException.html#v:evaluate">evaluate</a></code>. </p><p>Note that due to Haskell's unspecified evaluation order, an expression may return one of several possible exceptions: consider the expression <code>error "urk" + 1 `div` 0</code>. Does <code><a href="Control-OldException.html#v:catch">catch</a></code> execute the handler passing <code>ErrorCall "urk"</code>, or <code>ArithError DivideByZero</code>? </p><p>The answer is "either": <code><a href="Control-OldException.html#v:catch">catch</a></code> makes a non-deterministic choice about which exception to catch. If you call it again, you might get a different exception back. This is ok, because <code><a href="Control-OldException.html#v:catch">catch</a></code> is an <code><a href="System-IO.html#t:IO">IO</a></code> computation. </p><p>Note that <code><a href="Control-OldException.html#v:catch">catch</a></code> catches all types of exceptions, and is generally used for "cleaning up" before passing on the exception using <code><a href="Control-OldException.html#v:throwIO">throwIO</a></code>. It is not good practice to discard the exception and continue, without first checking the type of the exception (it might be a <code>ThreadKilled</code>, for example). In this case it is usually better to use <code><a href="Control-OldException.html#v:catchJust">catchJust</a></code> and select the kinds of exceptions to catch. </p><p>Also note that the <a href="Prelude.html">Prelude</a> also exports a function called <code>Prelude.catch</code> with a similar type to <code><a href="Control-OldException.html#v:catch">catch</a></code>, except that the <a href="Prelude.html">Prelude</a> version only catches the IO and user families of exceptions (as required by Haskell 98). </p><p>We recommend either hiding the <a href="Prelude.html">Prelude</a> version of <code>Prelude.catch</code> when importing <a href="Control-OldException.html">Control.OldException</a>: </p><pre> import Prelude hiding (catch) </pre><p>or importing <a href="Control-OldException.html">Control.OldException</a> qualified, to avoid name-clashes: </p><pre> import qualified Control.OldException as C </pre><p>and then using <code>C.catch</code> </p></div></div><div class="top"><p class="src"><a name="v:catchJust" class="def">catchJust</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> b)</td><td class="doc"><p>Predicate to select exceptions </p></td></tr><tr><td class="src">-> <a href="System-IO.html#t:IO">IO</a> a</td><td class="doc"><p>Computation to run </p></td></tr><tr><td class="src">-> (b -> <a href="System-IO.html#t:IO">IO</a> a)</td><td class="doc"><p>Handler </p></td></tr><tr><td class="src">-> <a href="System-IO.html#t:IO">IO</a> a</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>The function <code><a href="Control-OldException.html#v:catchJust">catchJust</a></code> is like <code><a href="Control-OldException.html#v:catch">catch</a></code>, but it takes an extra argument which is an <em>exception predicate</em>, a function which selects which type of exceptions we're interested in. There are some predefined exception predicates for useful subsets of exceptions: <code><a href="Control-OldException.html#v:ioErrors">ioErrors</a></code>, <code><a href="Control-OldException.html#v:arithExceptions">arithExceptions</a></code>, and so on. For example, to catch just calls to the <code><a href="Prelude.html#v:error">error</a></code> function, we could use </p><pre> result <- catchJust errorCalls thing_to_try handler </pre><p>Any other exceptions which are not matched by the predicate are re-raised, and may be caught by an enclosing <code><a href="Control-OldException.html#v:catch">catch</a></code> or <code><a href="Control-OldException.html#v:catchJust">catchJust</a></code>. </p></div></div><h2 id="g:5">The <code>handle</code> functions </h2><div class="top"><p class="src"><a name="v:handle" class="def">handle</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>A version of <code><a href="Control-OldException.html#v:catch">catch</a></code> with the arguments swapped around; useful in situations where the code for the handler is shorter. For example: </p><pre> do handle (\e -> exitWith (ExitFailure 1)) $ ... </pre></div></div><div class="top"><p class="src"><a name="v:handleJust" class="def">handleJust</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> b) -> (b -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>A version of <code><a href="Control-OldException.html#v:catchJust">catchJust</a></code> with the arguments swapped around (see <code><a href="Control-OldException.html#v:handle">handle</a></code>). </p></div></div><h2 id="g:6">The <code>try</code> functions </h2><div class="top"><p class="src"><a name="v:try" class="def">try</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> (<a href="Data-Either.html#t:Either">Either</a> <a href="Control-OldException.html#t:Exception">Exception</a> a)</p><div class="doc"><p>Similar to <code><a href="Control-OldException.html#v:catch">catch</a></code>, but returns an <code><a href="Data-Either.html#t:Either">Either</a></code> result which is <code>(<code><a href="Data-Either.html#v:Right">Right</a></code> a)</code> if no exception was raised, or <code>(<code><a href="Data-Either.html#v:Left">Left</a></code> e)</code> if an exception was raised and its value is <code>e</code>. </p><pre> try a = catch (Right `liftM` a) (return . Left) </pre><p>Note: as with <code><a href="Control-OldException.html#v:catch">catch</a></code>, it is only polite to use this variant if you intend to re-throw the exception after performing whatever cleanup is needed. Otherwise, <code><a href="Control-OldException.html#v:tryJust">tryJust</a></code> is generally considered to be better. </p><p>Also note that <a href="System-IO-Error.html">System.IO.Error</a> also exports a function called <code>System.IO.Error.try</code> with a similar type to <code><a href="Control-OldException.html#v:try">try</a></code>, except that it catches only the IO and user families of exceptions (as required by the Haskell 98 <code>IO</code> module). </p></div></div><div class="top"><p class="src"><a name="v:tryJust" class="def">tryJust</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> b) -> <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> (<a href="Data-Either.html#t:Either">Either</a> b a)</p><div class="doc"><p>A variant of <code><a href="Control-OldException.html#v:try">try</a></code> that takes an exception predicate to select which exceptions are caught (c.f. <code><a href="Control-OldException.html#v:catchJust">catchJust</a></code>). If the exception does not match the predicate, it is re-thrown. </p></div></div><h2 id="g:7">The <code>evaluate</code> function </h2><div class="top"><p class="src"><a name="v:evaluate" class="def">evaluate</a> :: a -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>Forces its argument to be evaluated to weak head normal form when the resultant <code><a href="System-IO.html#t:IO">IO</a></code> action is executed. It can be used to order evaluation with respect to other <code><a href="System-IO.html#t:IO">IO</a></code> operations; its semantics are given by </p><pre> evaluate x `seq` y ==> y evaluate x `catch` f ==> (return $! x) `catch` f evaluate x >>= f ==> (return $! x) >>= f </pre><p><em>Note:</em> the first equation implies that <code>(evaluate x)</code> is <em>not</em> the same as <code>(return $! x)</code>. A correct definition is </p><pre> evaluate x = (return $! x) >>= return </pre></div></div><h2 id="g:8">The <code>mapException</code> function </h2><div class="top"><p class="src"><a name="v:mapException" class="def">mapException</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Control-OldException.html#t:Exception">Exception</a>) -> a -> a</p><div class="doc"><p>This function maps one exception into another as proposed in the paper "A semantics for imprecise exceptions". </p></div></div><h2 id="g:9">Exception predicates </h2><div class="doc"><p>These pre-defined predicates may be used as the first argument to <code><a href="Control-OldException.html#v:catchJust">catchJust</a></code>, <code><a href="Control-OldException.html#v:tryJust">tryJust</a></code>, or <code><a href="Control-OldException.html#v:handleJust">handleJust</a></code> to select certain common classes of exceptions. </p></div><div class="top"><p class="src"><a name="v:ioErrors" class="def">ioErrors</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="System-IO-Error.html#t:IOError">IOError</a></p></div><div class="top"><p class="src"><a name="v:arithExceptions" class="def">arithExceptions</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Control-OldException.html#t:ArithException">ArithException</a></p></div><div class="top"><p class="src"><a name="v:errorCalls" class="def">errorCalls</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Data-Char.html#t:String">String</a></p></div><div class="top"><p class="src"><a name="v:dynExceptions" class="def">dynExceptions</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Data-Dynamic.html#t:Dynamic">Dynamic</a></p></div><div class="top"><p class="src"><a name="v:assertions" class="def">assertions</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Data-Char.html#t:String">String</a></p></div><div class="top"><p class="src"><a name="v:asyncExceptions" class="def">asyncExceptions</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Control-OldException.html#t:AsyncException">AsyncException</a></p></div><div class="top"><p class="src"><a name="v:userErrors" class="def">userErrors</a> :: <a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="Data-Maybe.html#t:Maybe">Maybe</a> <a href="Data-Char.html#t:String">String</a></p></div><h1 id="g:10">Dynamic exceptions </h1><div class="doc"><p><a name="DynamicExceptions"></a> Because the <code><a href="Control-OldException.html#t:Exception">Exception</a></code> datatype is not extensible, there is an interface for throwing and catching exceptions of type <code><a href="Data-Dynamic.html#t:Dynamic">Dynamic</a></code> (see <a href="Data-Dynamic.html">Data.Dynamic</a>) which allows exception values of any type in the <code><a href="Data-Typeable.html#t:Typeable">Typeable</a></code> class to be thrown and caught. </p></div><div class="top"><p class="src"><a name="v:throwDyn" class="def">throwDyn</a> :: <a href="Data-Typeable.html#t:Typeable">Typeable</a> exception => exception -> b</p><div class="doc"><p>Raise any value as an exception, provided it is in the <code><a href="Data-Typeable.html#t:Typeable">Typeable</a></code> class. </p></div></div><div class="top"><p class="src"><a name="v:throwDynTo" class="def">throwDynTo</a> :: <a href="Data-Typeable.html#t:Typeable">Typeable</a> exception => <a href="Control-Concurrent.html#t:ThreadId">ThreadId</a> -> exception -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></p><div class="doc"><p>A variant of <code><a href="Control-OldException.html#v:throwDyn">throwDyn</a></code> that throws the dynamic exception to an arbitrary thread (GHC only: c.f. <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code>). </p></div></div><div class="top"><p class="src"><a name="v:catchDyn" class="def">catchDyn</a> :: <a href="Data-Typeable.html#t:Typeable">Typeable</a> exception => <a href="System-IO.html#t:IO">IO</a> a -> (exception -> <a href="System-IO.html#t:IO">IO</a> a) -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>Catch dynamic exceptions of the required type. All other exceptions are re-thrown, including dynamic exceptions of the wrong type. </p><p>When using dynamic exceptions it is advisable to define a new datatype to use for your exception type, to avoid possible clashes with dynamic exceptions used in other libraries. </p></div></div><h1 id="g:11">Asynchronous Exceptions </h1><div class="doc"><p><a name="AsynchronousExceptions"></a> Asynchronous exceptions are so-called because they arise due to external influences, and can be raised at any point during execution. <code>StackOverflow</code> and <code>HeapOverflow</code> are two examples of system-generated asynchronous exceptions. </p><p>The primary source of asynchronous exceptions, however, is <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code>: </p><pre> throwTo :: ThreadId -> Exception -> IO () </pre><p><code><a href="Control-OldException.html#v:throwTo">throwTo</a></code> (also <code><a href="Control-OldException.html#v:throwDynTo">throwDynTo</a></code> and <code>Control.Concurrent.killThread</code>) allows one running thread to raise an arbitrary exception in another thread. The exception is therefore asynchronous with respect to the target thread, which could be doing anything at the time it receives the exception. Great care should be taken with asynchronous exceptions; it is all too easy to introduce race conditions by the over zealous use of <code><a href="Control-OldException.html#v:throwTo">throwTo</a></code>. </p></div><h2 id="g:12">Asynchronous exception control </h2><div class="doc"><p>The following two functions allow a thread to control delivery of asynchronous exceptions during a critical region. </p></div><div class="top"><p class="src"><a name="v:block" class="def">block</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>Note: this function is deprecated, please use <code><a href="Control-Exception-Base.html#v:mask">mask</a></code> instead. </p><p>Applying <code><a href="Control-OldException.html#v:block">block</a></code> to a computation will execute that computation with asynchronous exceptions <em>blocked</em>. That is, any thread which attempts to raise an exception in the current thread with <code>Control.Exception.throwTo</code> will be blocked until asynchronous exceptions are unblocked again. There's no need to worry about re-enabling asynchronous exceptions; that is done automatically on exiting the scope of <code><a href="Control-OldException.html#v:block">block</a></code>. </p><p>Threads created by <code>Control.Concurrent.forkIO</code> inherit the blocked state from the parent; that is, to start a thread in blocked mode, use <code>block $ forkIO ...</code>. This is particularly useful if you need to establish an exception handler in the forked thread before any asynchronous exceptions are received. </p></div></div><div class="top"><p class="src"><a name="v:unblock" class="def">unblock</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> a</p><div class="doc"><p>Note: this function is deprecated, please use <code><a href="Control-Exception-Base.html#v:mask">mask</a></code> instead. </p><p>To re-enable asynchronous exceptions inside the scope of <code><a href="Control-OldException.html#v:block">block</a></code>, <code><a href="Control-OldException.html#v:unblock">unblock</a></code> can be used. It scopes in exactly the same way, so on exit from <code><a href="Control-OldException.html#v:unblock">unblock</a></code> asynchronous exception delivery will be disabled again. </p></div></div><h3 id="g:13">Applying <code>block</code> to an exception handler </h3><div class="doc"><p>There's an implied <code>mask_</code> around every exception handler in a call to one of the <code><a href="Control-OldException.html#v:catch">catch</a></code> family of functions. This is because that is what you want most of the time - it eliminates a common race condition in starting an exception handler, because there may be no exception handler on the stack to handle another exception if one arrives immediately. If asynchronous exceptions are blocked on entering the handler, though, we have time to install a new exception handler before being interrupted. If this weren't the default, one would have to write something like </p><pre> mask $ \restore -> catch (restore (...)) (\e -> handler) </pre><p>If you need to unblock asynchronous exceptions again in the exception handler, just use <code><a href="Control-OldException.html#v:unblock">unblock</a></code> as normal. </p><p>Note that <code><a href="Control-OldException.html#v:try">try</a></code> and friends <em>do not</em> have a similar default, because there is no exception handler in this case. If you want to use <code><a href="Control-OldException.html#v:try">try</a></code> in an asynchronous-exception-safe way, you will need to use <code><a href="Control-Exception-Base.html#v:mask">mask</a></code>. </p></div><h3 id="g:14">Interruptible operations </h3><div class="doc"><p>Some operations are <em>interruptible</em>, which means that they can receive asynchronous exceptions even in the scope of a <code><a href="Control-Exception-Base.html#v:mask">mask</a></code>. Any function which may itself block is defined as interruptible; this includes <code>Control.Concurrent.MVar.takeMVar</code> (but not <code>Control.Concurrent.MVar.tryTakeMVar</code>), and most operations which perform some I/O with the outside world. The reason for having interruptible operations is so that we can write things like </p><pre> mask $ \restore -> do a <- takeMVar m catch (restore (...)) (\e -> ...) </pre><p>if the <code>Control.Concurrent.MVar.takeMVar</code> was not interruptible, then this particular combination could lead to deadlock, because the thread itself would be blocked in a state where it can't receive any asynchronous exceptions. With <code>Control.Concurrent.MVar.takeMVar</code> interruptible, however, we can be safe in the knowledge that the thread can receive exceptions right up until the point when the <code>Control.Concurrent.MVar.takeMVar</code> succeeds. Similar arguments apply for other interruptible operations like <code>System.IO.openFile</code>. </p></div><h1 id="g:15">Assertions </h1><div class="top"><p class="src"><a name="v:assert" class="def">assert</a> :: <a href="Data-Bool.html#t:Bool">Bool</a> -> a -> a</p><div class="doc"><p>If the first argument evaluates to <code><a href="Data-Bool.html#v:True">True</a></code>, then the result is the second argument. Otherwise an <code>AssertionFailed</code> exception is raised, containing a <code><a href="Data-Char.html#t:String">String</a></code> with the source file and line number of the call to <code><a href="Control-OldException.html#v:assert">assert</a></code>. </p><p>Assertions can normally be turned on or off with a compiler flag (for GHC, assertions are normally on unless optimisation is turned on with <code>-O</code> or the <code>-fignore-asserts</code> option is given). When assertions are turned off, the first argument to <code><a href="Control-OldException.html#v:assert">assert</a></code> is ignored, and the second argument is returned as the result. </p></div></div><h1 id="g:16">Utilities </h1><div class="top"><p class="src"><a name="v:bracket" class="def">bracket</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="System-IO.html#t:IO">IO</a> a</td><td class="doc"><p>computation to run first ("acquire resource") </p></td></tr><tr><td class="src">-> (a -> <a href="System-IO.html#t:IO">IO</a> b)</td><td class="doc"><p>computation to run last ("release resource") </p></td></tr><tr><td class="src">-> (a -> <a href="System-IO.html#t:IO">IO</a> c)</td><td class="doc"><p>computation to run in-between </p></td></tr><tr><td class="src">-> <a href="System-IO.html#t:IO">IO</a> c</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>When you want to acquire a resource, do some work with it, and then release the resource, it is a good idea to use <code><a href="Control-OldException.html#v:bracket">bracket</a></code>, because <code><a href="Control-OldException.html#v:bracket">bracket</a></code> will install the necessary exception handler to release the resource in the event that an exception is raised during the computation. If an exception is raised, then <code><a href="Control-OldException.html#v:bracket">bracket</a></code> will re-raise the exception (after performing the release). </p><p>A common example is opening a file: </p><pre> bracket (openFile "filename" ReadMode) (hClose) (\handle -> do { ... }) </pre><p>The arguments to <code><a href="Control-OldException.html#v:bracket">bracket</a></code> are in this order so that we can partially apply it, e.g.: </p><pre> withFile name mode = bracket (openFile name mode) hClose </pre></div></div><div class="top"><p class="src"><a name="v:bracket_" class="def">bracket_</a> :: <a href="System-IO.html#t:IO">IO</a> a -> <a href="System-IO.html#t:IO">IO</a> b -> <a href="System-IO.html#t:IO">IO</a> c -> <a href="System-IO.html#t:IO">IO</a> c</p><div class="doc"><p>A variant of <code><a href="Control-OldException.html#v:bracket">bracket</a></code> where the return value from the first computation is not required. </p></div></div><div class="top"><p class="src"><a name="v:bracketOnError" class="def">bracketOnError</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="System-IO.html#t:IO">IO</a> a</td><td class="doc"><p>computation to run first ("acquire resource") </p></td></tr><tr><td class="src">-> (a -> <a href="System-IO.html#t:IO">IO</a> b)</td><td class="doc"><p>computation to run last ("release resource") </p></td></tr><tr><td class="src">-> (a -> <a href="System-IO.html#t:IO">IO</a> c)</td><td class="doc"><p>computation to run in-between </p></td></tr><tr><td class="src">-> <a href="System-IO.html#t:IO">IO</a> c</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>Like bracket, but only performs the final action if there was an exception raised by the in-between computation. </p></div></div><div class="top"><p class="src"><a name="v:finally" class="def">finally</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="System-IO.html#t:IO">IO</a> a</td><td class="doc"><p>computation to run first </p></td></tr><tr><td class="src">-> <a href="System-IO.html#t:IO">IO</a> b</td><td class="doc"><p>computation to run afterward (even if an exception was raised) </p></td></tr><tr><td class="src">-> <a href="System-IO.html#t:IO">IO</a> a</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>A specialised variant of <code><a href="Control-OldException.html#v:bracket">bracket</a></code> with just a computation to run afterward. </p></div></div><div class="top"><p class="src"><a name="v:setUncaughtExceptionHandler" class="def">setUncaughtExceptionHandler</a> :: (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a>) -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a></p></div><div class="top"><p class="src"><a name="v:getUncaughtExceptionHandler" class="def">getUncaughtExceptionHandler</a> :: <a href="System-IO.html#t:IO">IO</a> (<a href="Control-OldException.html#t:Exception">Exception</a> -> <a href="System-IO.html#t:IO">IO</a> <a href="../ghc-prim-0.2.0.0/GHC-Unit.html#t:-40--41-">()</a>)</p></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.9.2</p></div></body></html>