[Haskell-cafe] Catching an escaping exception

Joel Reymont joelr1 at gmail.com
Fri Nov 4 07:15:30 EST 2005


How can I catch this exception and translate it into an error within  
my monad?

The exception is going to ghci and is not being caught in the monad.  
I'm interested in catching it and failing the monad gracefully.

Exception:

<interactive>: connect: does not exist (Connection refused)

Types follow...

type EngineState = ErrorT String (StateT World IO)
type EngineResult = IO (Either String (), World)

connect :: [Prop] -> HostName -> Int -> EngineState ()
connect env h p =
     do w <- get
        let secs = timeout_seconds w
        (Right h) <- liftIO $ timeout secs $ connect_ h p
        trace "Connection established"
...

connect_ :: HostName -> Int -> IO Handle
connect_ h p = connectTo h $ PortNumber $ fromIntegral p

timeout :: forall a.Int -> IO a -> IO (Either String a)
timeout seconds action =
...

	Thanks, Joel

--
http://wagerlabs.com/







More information about the Haskell-Cafe mailing list