I have a question about the swift error handling. In my swift Script I have to do multiple things with the FileManager that could throw an exception. Now my first thought was, to put them all in one do-catch block.
do {
let fileManager = FileManager.default
try fileManager.moveItem(atPath: destination, toPath: "\(destination).old")
try fileManager.createDirectory(atPath: destination, withIntermediateDirectories: false)
...
} catch {
print(error)
exit(EXIT_FAILURE)
}
The problem now is, that I cannot determine in the catch block, which statement threw the error. The localizedDescription is not really helpful either ("Error while restoring backup!").
Also I cannot really find out, which type the thrown error is, because I don’t find anything to it in the FileManager documentation.
I guess a way that works, would be to put each statement in it‘s own nested do-catch block, but this looks in my opinion very messy and hard to read.
So my question is, if there is another way to determine the error type or the statement that threw it in the catch block or to find out, which error type each FileManager statement throws?
Thanks in advance, Jonas