Exception handling with bracket
06 Jan 2013Introduction
Performing IO in our pure environments can be dangerous. Who knows what people put in files that our programs are expected to read? There are tools that can help assure ourselves that we’ll at least clean up if an explosion occurs. In this post, I’ll talk about exception handling with IO
actions using bracket.
What is it?
bracket is a function defined in Control.Exception
and is defined as follows.
Its first parameter is a function that acquires a resource (or handle). Its second parameter is a function that releases the resource acquired in the first. The last parameter is another function that uses the acquired handle to get a result. The error handling comes in because the 2nd parameter, the function that releases the acquired resource is called even if an exception occurs during the execution of the last parameter. That’s nifty. An example use of bracket looks as follows.
So, there’s another safety mat for when you venture into the impure world.