Re: Teach me to cooperate with IOException
Henning Thielemann <[email protected]>
| Newsgroups | gmane.comp.lang.haskell.cafe |
|---|---|
| Message-ID | <[email protected]> |
On Tue, 16 Apr 2024, Folsk Pratima wrote: > foreign import ccall unsafe "complex.c syscall_that_fails" c_syscall_that_fails > :: Ptr CInt -> IO CInt > > sysCall :: Int -> IO () > sysCall i = > allocaBytes (sizeOf (0 :: CInt)) $ \ptr -> do > st <- c_syscall_that_fails ptr > errno <- peekElemOff ptr 0 > case st of > (-1) -> throwIO $ userError $ "syscall: " ++ show errno > _ -> return () > > ``` > > Now, I do not think it is a "user error", it is a real system call > error that might or might not be already covered by System.IO.Error. In > my case errno is 2, it is "No such file or directory", so it is covered > by System.IO.Error and I may just use the library. But my C code may > call whatever! Generally I discourage using the exceptions implicit in IO. Exceptions must be properly handled by the function caller, the caller must know what exceptions can occur. Thus I prefer using Exception monad transformers like transformers:Trans.ExceptT or explicit-exception:Trans.Exception.Synchronous. _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.