title: Prevent NSKeyedArchiver throwing exception without Objective-C
body: <p>On iOS version lower than 11 the throwing <code>archivedData(withRootObject:requiringSecureCoding:)</code> is unavailable, so I have tried to do the equivalent on versions less than iOS 11:</p>
  
  <pre class="lang-swift prettyprint-override"><code>let archiveData: NSData
  if #available(iOS 11.0, *) {
      archiveData = try NSKeyedArchiver.archivedData(
          withRootObject: rootObject,
          requiringSecureCoding: true
      ) as NSData
  } else {
      NSKeyedArchiver.archivedData(withRootObject: userActivity)
      let mutableData = NSMutableData()
      let archiver = NSKeyedArchiver(forWritingWith: mutableData)
      archiver.requiresSecureCoding = true
      archiver.encode(rootObject, forKey: NSKeyedArchiveRootObjectKey)
      if let error = archiver.error {
          throw error
      }
      archiver.finishEncoding()
      archiveData = mutableData
  }
  </code></pre>
  
  <p>However, when the <code>rootObject</code> calls <code>NSCoder.failWithError(_:)</code> in the <code>encode(with:)</code> function an <code>NSInvalidUnarchiveOperationException</code> exception is raised.</p>
  
  <p>If I subclass <code>NSKeyedArchiver</code> as such:</p>
  
  <pre class="lang-swift prettyprint-override"><code>final class KeyedArchiver: NSKeyedArchiver {
  
      override var decodingFailurePolicy: NSCoder.DecodingFailurePolicy {
          return .setErrorAndReturn
      }
  
  }
  </code></pre>
  
  <p>It instead raises an <code>NSInternalInconsistencyException</code> exception with the message <code>Attempting to set decode error on throwing NSCoder</code>.</p>
  
  <p>Is there a way to do this kind of archiving without throwing an exception, short of writing an Objective-C function to catch the exception and throwing it as an error?</p>