[Haskell-cafe] Uncatchable error

Henning Thielemann lemming at henning-thielemann.de
Fri Mar 11 20:03:11 CET 2011

On Fri, 11 Mar 2011, Daniel Fischer wrote:

> On Friday 11 March 2011 17:04:16, Daniel Díaz wrote:
>> Hi, cafe,
>> I'm working in a program where I use many connections with Network.HTTP.
>> Sometimes, connections are closed while my program is reading them, and
>> an error appears:
>> <socket: XXX>: Data.ByteString.hGetLine: invalid argument (Bad file
>> descriptor)
>> All I need is to handle this error. The function 'catch' doesn't work. I
>> guess this error comes from a call to 'error' function, or something
>> similar.
> Control.Exception.catch can catch error calls. If you're using
> Prelude.catch, you probably should switch.

Using 'error' in such cases would be definitely wrong, since a connection 
closed by the other end of the connection is not a programming error but 
an exceptional situation that we must handle properly at runtime.

Unfortunately it is not documented in bytestring package, what exceptions 
hGetLine can throw and the type IO does not tell us, too.
(For alternatives see: 

You may scan the exceptions in
  for types that may be responsible for invalid file descriptors. This 
should certainly be in IOException, but on the other hand, IOException is 
also what Prelude.catch catches.

More information about the Haskell-Cafe mailing list