Module
Effect.Exception
- Package
- purescript-exceptions
- Repository
- purescript/purescript-exceptions
This module defines an effect, actions and handlers for working with JavaScript exceptions.
#catchException Source
catchException :: forall a. (Error -> Effect a) -> Effect a -> Effect a
Catch an exception by providing an exception handler.
For example:
main = catchException Console.logShow do
Console.log "Exceptions thrown in this block will be logged to the console"
#errorWithCause Source
errorWithCause :: String -> Error -> Error
Create a JavaScript error, specifying a message and a cause
#errorWithName Source
errorWithName :: String -> String -> Error
Create a JavaScript error, specifying a message and a name
#throwException Source
throwException :: forall a. Error -> Effect a
Throw an exception
For example:
main = do
x <- readNumber
when (x < 0) $ throwException $
error "Expected a non-negative number"
#try Source
try :: forall a. Effect a -> Effect (Either Error a)
Runs an Eff and returns eventual Exceptions as a Left
value. If the
computation succeeds the result gets wrapped in a Right
.
For example:
main :: Effect Unit
main = do
result <- try (readTextFile UTF8 "README.md")
case result of
Right lines ->
Console.log ("README: \n" <> lines )
Left error ->
Console.error ("Couldn't open README.md. Error was: " <> show error)