If the first argument evaluates to True, then the result is the
second argument. Otherwise an AssertionFailed exception
is raised, containing a String with the source file and line number of the
call to assert.
Assertions can normally be turned on or off with a compiler flag
(for GHC, assertions are normally on unless optimisation is turned on
with -O or the -fignore-asserts option is given). When assertions
are turned off, the first argument to assert is ignored, and the second
argument is returned as the result.
Displays a SomeException with additional information:
The type of the underlying exception
The exception context
By default, uncaughtExceptionHandler uses displayExceptionWithInfo to print uncaught exceptions.
This default can be overridden with setUncaughtExceptionHandler, for
instance, to present custom error messages on exceptions to the user.
A variant of catch which doesn't annotate the handler with the exception
which was caught. This function should be used when you are implementing your own
error handling functions which may rethrow the exceptions.
In the case where you rethrow an exception without modifying it, you should
rethrow the exception with the old exception context.
Allow asynchronous exceptions to be raised even inside mask, making
the operation interruptible (see the discussion of "Interruptible operations"
in Control.Exception).
A class method without a definition (neither a default definition,
nor a definition in the appropriate instance) was called. The
String gives information about which method it was.
Thrown when the runtime system detects that the computation is
guaranteed not to terminate. Note that there is no guarantee that
the runtime system will notice whether any given computation is
guaranteed to terminate or not.
A record selector was applied to a constructor without the
appropriate field. This can only happen with a datatype with
multiple constructors, where some fields are in one constructor
but not another. The String gives information about the source
location of the record selector.
A record update was performed on a constructor without the
appropriate field. This can only happen with a datatype with
multiple constructors, where some fields are in one constructor
but not another. The String gives information about the source
location of the record update.
An expression that didn't typecheck during compile time was called.
This is only possible with -fdefer-type-errors. The String gives
details about the failed type check.
Deprecated. ErrorCallWithLocation has been deprecated in favour of ErrorCall (which does not have a location). Backtraces are now handled by the backtrace exception mechanisms exclusively.
Any type that you wish to throw or catch as an exception must be an
instance of the Exception class. The simplest case is a new exception
type directly below the root:
data MyException = ThisException | ThatException
deriving Show
instance Exception MyException
The default method definitions in the Exception class do what we need
in this case. You can now throw and catch ThisException and
ThatException as exceptions:
*Main> throw ThisException `catch` \e -> putStrLn ("Caught " ++ show (e :: MyException))
Caught ThisException
In more complicated examples, you may wish to define a whole hierarchy
of exceptions:
---------------------------------------------------------------------
-- Make the root exception type for all the exceptions in a compiler
data SomeCompilerException = forall e . Exception e => SomeCompilerException e
instance Show SomeCompilerException where
show (SomeCompilerException e) = show e
instance Exception SomeCompilerException
compilerExceptionToException :: Exception e => e -> SomeException
compilerExceptionToException = toException . SomeCompilerException
compilerExceptionFromException :: Exception e => SomeException -> Maybe e
compilerExceptionFromException x = do
SomeCompilerException a <- fromException x
cast a
---------------------------------------------------------------------
-- Make a subhierarchy for exceptions in the frontend of the compiler
data SomeFrontendException = forall e . Exception e => SomeFrontendException e
instance Show SomeFrontendException where
show (SomeFrontendException e) = show e
instance Exception SomeFrontendException where
toException = compilerExceptionToException
fromException = compilerExceptionFromException
frontendExceptionToException :: Exception e => e -> SomeException
frontendExceptionToException = toException . SomeFrontendException
frontendExceptionFromException :: Exception e => SomeException -> Maybe e
frontendExceptionFromException x = do
SomeFrontendException a <- fromException x
cast a
---------------------------------------------------------------------
-- Make an exception type for a particular frontend compiler exception
data MismatchedParentheses = MismatchedParentheses
deriving Show
instance Exception MismatchedParentheses where
toException = frontendExceptionToException
fromException = frontendExceptionFromException
We can now catch a MismatchedParentheses exception as
MismatchedParentheses, SomeFrontendException or
SomeCompilerException, but not other types, e.g. IOException:
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: MismatchedParentheses))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeFrontendException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeCompilerException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: IOException))
*** Exception: MismatchedParentheses
In most cases, the exception does not store its own ExceptionContext.
The default implementation of toException (which does not store any
ExceptionContext) is suitable for these cases.
In the rare case that the exception itself stores an ExceptionContext,
this context should be preserved by toException.
Wraps a particular exception exposing its ExceptionContext. Intended to
be used when catching exceptions in cases where access to the context is
desired.
The SomeException type is the root of the exception type hierarchy.
When an exception of type e is thrown, behind the scenes it is
encapsulated in a SomeException.
WhileHandling is used to annotate rethrow exceptions. By inspecting
the WhileHandling annotation, all the places the exception has been rethrow
can be recovered.
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.
The program's heap is reaching its limit, and
the program should take action to reduce the amount of
live data it has. Notes:
It is undefined which thread receives this exception.
GHC currently throws this to the same thread that
receives UserInterrupt, but this may change in the
future.
The GHC RTS currently can only recover from heap overflow
if it detects that an explicit memory limit (set via RTS flags).
has been exceeded. Currently, failure to allocate memory from
the operating system results in immediate termination of the
program.
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).
Instances4Eq, Ord, Show, Exception
EqAsyncExceptionDefined in ghc-internal-10.0.0 · GHC.Internal.IO.Exception
OrdAsyncExceptionDefined in ghc-internal-10.0.0 · GHC.Internal.IO.Exception
ShowAsyncExceptionDefined in ghc-internal-10.0.0 · GHC.Internal.IO.Exception
Exceptions that occur in the IO monad.
An IOException records a more specific error type, a descriptive
string and maybe the handle that was used when the error was
flagged.
Instances3Eq, Show, Exception
EqIOExceptionDefined in ghc-internal-10.0.0 · GHC.Internal.IO.Exception
ShowIOExceptionDefined in ghc-internal-10.0.0 · GHC.Internal.IO.Exception
When the "acquire" or "release" computations throw exceptions
any monadic side effects in m will be discarded.
When the "in-between" computation throws an exception any
monadic side effects in m produced by that computation will be
discarded but the side effects of the "acquire" or "release"
computations will be retained.
Also, any monadic side effects in m of the "release"
computation will be discarded; it is run only for its side
effects in IO.
Note that when your acquire and release computations are of type IO
it will be more efficient to write:
Note any monadic side effects in m of both the "acquire" and
"release" computations will be discarded. To keep the monadic
side effects of the "acquire" computation, use bracket with
constant functions instead.
Note that when your acquire and release computations are of type IO
it will be more efficient to write:
When the "acquire" or "release" computations throw exceptions
any monadic side effects in m will be discarded.
When the "in-between" computation throws an exception any
monadic side effects in m produced by that computation will be
discarded but the side effects of the "acquire" computation
will be retained.
Also, any monadic side effects in m of the "release"
computation will be discarded; it is run only for its side
effects in IO.
Note that when your acquire and release computations are of
type IO it will be more efficient to write: