Haskell - exit a program with a specified error code

Use exitWith from System.Exit:

main = exitWith (ExitFailure 2)

I would add some helpers for convenience:

exitWithErrorMessage :: String -> ExitCode -> IO a
exitWithErrorMessage str e = hPutStrLn stderr str >> exitWith e

exitResourceMissing :: IO a
exitResourceMissing = exitWithErrorMessage "Resource missing" (ExitFailure 2)

An alternative that allows an error message only is die

import System.Exit

tests = ... -- some value from the program
testsResult = ... -- Bool value overall status

main :: IO ()
main = do
    if testsResult then
        print "Tests passed"
    else
        die (show tests)

The accepted answer allows setting the exit error code though, so it's closer to the exact phrasing of the question.