
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.