title: Catch NSFileHandleOperationException in Swift
body: <p>How can I catch a <code>NSFileHandleOperationException</code> in Swift?</p>
  <p>I use <code>fileHandle.readDataToEndOfFile()</code> which calls (according to the documentation) <code>fileHandle.readDataOfLength()</code> which can throw (again according to the documentation) a <code>NSFileHandleOperationException</code>.</p>
  <p>How can I catch this exception? I tried</p>
  <pre><code>do {
      return try fH.readDataToEndOfFile()
  } catch NSFileHandleOperationException {
      return nil
  }
  </code></pre>
  <p>but Xcode says</p>
  <blockquote>
  <p>Warning: No calls to throwing functions occur within 'try' expression</p>
  <p>Warning: 'catch' block is unreachable because no errors are thrown in 'do' block</p>
  </blockquote>
  <p>How do I do this? </p>
  <p>Edit:
  I just decided to use C's good old <code>fopen</code>, <code>fread</code>, <code>fclose</code> as a workaround:</p>
  <pre><code>extension NSMutableData {
      public enum KCStd$createFromFile$err: ErrorType {
          case Opening, Reading, Length
      }
      
      public static func KCStd$createFromFile(path: String, offset: Int = 0, length: Int = 0) throws -&gt; NSMutableData {
          let fh = fopen(NSString(string: path).UTF8String, NSString(string: &quot;r&quot;).UTF8String)
          if fh == nil { throw KCStd$createFromFile$err.Opening }
          defer { fclose(fh) }
      
          fseek(fh, 0, SEEK_END)
          let size = ftell(fh)
          fseek(fh, offset, SEEK_SET)
      
          let toRead: Int
          if length &lt;= 0 {
              toRead = size - offset
          } else if offset + length &gt; size {
              throw KCStd$createFromFile$err.Length
          } else {
              toRead = length
          }
      
          let buffer = UnsafeMutablePointer&lt;UInt8&gt;.alloc(toRead)
          defer {
              memset_s(buffer, toRead, 0x00, toRead)
              buffer.destroy(toRead)
              buffer.dealloc(toRead)
          }
          let read = fread(buffer, 1, toRead, fh)
          if read == toRead {
              return NSMutableData(bytes: buffer, length: toRead)
          } else {
              throw KCStd$createFromFile$err.Reading
          }
      }
  }
  </code></pre>
  <p><code>KCStd$</code> (abbreviation for KizzyCode Standard Library) is the prefix because extensions are module-wide. The above code is hereby placed in Public Domain </p>
  <p>I'll leave this open because it's nonetheless an interesting question.</p>