I'm writing some Swift code that opens PDF files inline using a 3rd party control. The problem is, for a small (but non-negligible) portion of PDF documents, the control encounters an "unexpectedly found nil" due to certain metadata being missing from the PDF document. Apparently try/catch only works in swift for errors that are explicitly declared by the programmer - nevertheless here is the code I've tried (which warns that no exception is thrown)
var document: PDFDocument
do{
// exception is happening in this constructor
try document = PDFDocument(filePath: location.path, password: nil)
}catch{
// fallback to using iOS defaults
let av = UIAlertController(title: "Error", message: "This document couldn't be opened. You will now be directed to your phone's default application for handling this file.", preferredStyle: .alert)
av.addAction(UIAlertAction(title: "OK", style: .default, handler: {(alert: UIAlertAction!) in
let docController = UIDocumentInteractionController(url: location)
...
}))
return;
}
Approaches I've considered:
- Try/catch - but the catch block is never run due to reasons mentioned above
- Override the 3rd party code - my most promising option, but due to the way the code is written the override would be pretty gnarly and I'd like to avoid it if possible
- Manipulate the PDF document on the fly to not throw the exception - maybe, but might be expensive for large PDF files
Is there a way to arbitrarily catch errors like these? Or is an override the best approach?
I'm fairly new to Swift so it may be my ignorance, but this seems like a pretty big gap compared to other platforms I've used.
Edit: For what its worth, the 3rd party library is something called UXMPDFKit
Thanks,