title: The "handle" function and Real World Haskell
body: <p>I'm reading RWH, and I've come to chapter 9. It introduces the following piece of code:</p>
  
  <pre><code>import System.IO
  import Control.Exception
  
  saferFileSize :: FilePath -&gt; IO (Maybe Integer)
  saferFileSize path = handle (\_ -&gt; return Nothing) $ do
    h &lt;- openFile path ReadMode
    size &lt;- hFileSize h
    hClose h
    return (Just size)
  </code></pre>
  
  <p>It won't compile however, giving the following error message:</p>
  
  <pre><code>test.hs:5:22:
      Ambiguous type variable `e0' in the constraint:
        (Exception e0) arising from a use of `handle'
      Probable fix: add a type signature that fixes these type variable(s)
      In the expression: handle (\ _ -&gt; return Nothing)
      In the expression:
        handle (\ _ -&gt; return Nothing)
        $ do { h &lt;- openFile path ReadMode;
               size &lt;- hFileSize h;
               hClose h;
               return (Just size) }
      In an equation for `saferFileSize':
          saferFileSize path
            = handle (\ _ -&gt; return Nothing)
              $ do { h &lt;- openFile path ReadMode;
                     size &lt;- hFileSize h;
                     hClose h;
                     .... }
  </code></pre>
  
  <p>What is going wrong here? Why won't it compile?</p>