Custom Errors in Haskell
In Haskell, we can create custom error types using data constructors. Here’s an example that demonstrates how to create and use custom errors:
import Control.Exception (Exception, throw, catch)
import Text.Printf (printf)
-- A custom error type usually has the suffix "Error".
data ArgError = ArgError
{ arg :: Int
, message :: String
} deriving (Show)
-- Make ArgError an instance of Exception
instance Exception ArgError
-- Function that may throw our custom error
f :: Int -> Either ArgError Int
f arg
| arg == 42 = Left $ ArgError arg "can't work with it"
| otherwise = Right $ arg + 3
main :: IO ()
main = do
-- Using `catch` to handle our custom error
result <- catch (case f 42 of
Left e -> throw e
Right val -> return val)
(\e -> do
let err = e :: ArgError
putStrLn $ printf "%d - %s" (arg err) (message err)
return (-1))
print resultIn this Haskell version:
We define a custom
ArgErrortype withargandmessagefields.We make
ArgErroran instance of theExceptiontypeclass, which allows it to be thrown and caught.The
ffunction returns anEither ArgError Int. It returnsLeft ArgErrorwhen the input is 42, otherwise it returnsRight (arg + 3).In the
mainfunction, we usecatchto handle our custom error. If anArgErroris thrown, we print its contents.Haskell doesn’t have a direct equivalent to Go’s
errors.As, but we can achieve similar functionality using pattern matching in the catch clause.
To run the program:
$ ghc custom_errors.hs
$ ./custom_errors
42 - can't work with it
-1This Haskell implementation showcases how to create and use custom error types, providing functionality similar to the original example while using idiomatic Haskell constructs.