liveBook · Manning

Exception

As explained in Functional Design and Architecture: Examples in Haskell

A synchronous exception is an exception raised and handled within the execution flow of the thread that encounters the abnormal condition.

An exception is a language-dependent mechanism that interrupts normal control flow when an unexpected condition occurs. It can be caught; if it is not, the application may terminate. A synchronous exception arises within the execution flow of the thread encountering the condition, unlike a Haskell asynchronous exception, which can occur in another thread. section 13.1.1

Exceptions are often implicit and associated with unexpected conditions or bugs. By contrast, an error can represent an anticipated failure as an explicit value. The distinction is a design guideline rather than a universal rule: languages and projects sometimes use exceptions for expected errors or prohibit them altogether. section 13.1.1 section 13.3

An exception from a native library can expose implementation details to business logic. One way to limit that coupling is to catch it at an architectural boundary and convert it into an error value understood by the next layer. Different error domains can use different representations, with conversion at their boundaries. section 13.1.1 section 13.1.3 section 13.3

Filesystem failure as a value

A filesystem interpreter can catch an IOException from writeFile and return an Either containing a filesystem error or a successful result. Conversion can happen in the interpreter or in the free language's smart constructor. The latter can expose a native-result helper, so hiding that helper may prevent clients from depending on implementation details. section 13.1.3

Listing 13.2 Converting exceptions into errors at the interpreter level
data FileSystemF next where
  WriteFile :: FilePath -> StringToWrite
            -> (FSResult () -> next)                         #1
            -> FileSystemF next
  
writeFile' :: FilePath -> StringToWrite
           -> FileSystemL (FSResult ())                      #2
writeFile' path content = liftF (WriteFile path content id)
  
interpretFileSystemF :: FileSystemF a -> IO a
interpretFileSystemF (WriteFile path content next) = do
  eRes <- try (writeFile path content)                       #3
  pure (next (fromNativeResult eRes))                        #4

The interpreter catches the native exception and converts the result before passing it back to the free monadic language. section 13.1.3

A caught exception can also be discarded, but doing so hides failure. For example, a write operation that returns only () after suppressing an exception cannot guarantee that the write succeeded. section 13.1.3

Catching inside a free monadic scenario

If database initialization converts a failure into a thrown exception, the free monadic program stops and the exception can be caught around its evaluation. To handle it within the scenario instead, a runSafely operation can interpret a nested action and return an Either. Haskell's try cannot be applied directly to the scenario action because try expects IO. section 13.1.4

Listing 13.5 The interpreter for the new methods
import Control.Exception (throwIO)
  
interpretLangF :: LangF a -> IO a
interpretLangF (ThrowException exc _) = throwIO exc #1
interpretLangF (RunSafely act next) = do
  eResult <- try (runLangL act)                     #2
  pure (next (case eResult of
    Left err -> Left err
    Right r  -> Right r
  ))
  
runLangL :: LangL a -> IO a
runLangL = foldFree interpretLangF

The interpreter implements throwing with throwIO and runs the nested action under try, returning its result to the scenario. section 13.1.4

For an expected failure, an explicit result can also make an interface safer. A device-creation method that throws when a component is missing could instead return Either DeviceCreationError Device, making that failure visible to callers. section 4.2.3

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains exception flow and boundary conversion through interpreter implementations, including catching within a free monadic scenario.
Examples
Uses filesystem writes, database initialization, and missing hardware components to illustrate failure handling.
What you can do after
Apply Haskell's try and a runSafely interpreter operation to catch exceptions and return explicit results.
How it connects
Connects exceptions with error handling, dependency inversion, free monads, interpreters, and Either.

Also explained in

Compare how 25 books explain exception →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.