Super great question and they answer may not be so obvious, but here’s an attempt at a high level answer.
The Do-Catch syntax is designed to capture errors during program execution and help recover from them - and is really a Swift implementation.
What’s happening here is a programmer error - attempting to delete a non-managed object from a managed Realm is a pre-condition error and really, not recoverable. Attempting it again will produce the same error. e.g. it’s an ObjC exception due to a coding error, so fix your code
That being said the documentation (IMO) is pretty weak/vague on this; realm.write says things like:
and trying to begin a write transaction on a Realm which is already in a write transaction will throw an exception.
NSError if the transaction could not be completed successfully. If
block throws, the function throws the propagated
But it’s not clear under what conditions it will throw (pretty much none) or that its “throwing” is not what many of us consider “throwing” in a Swfit context.
So, in a nutshell, Realm doesn’t validate the quality of the data, just that its data.
That can get you into potential trouble: Given a Person has one property “name”; trying to populate it with “data” containing “field0” and “field1” is a valid call but what’s contained in “data” is not
let data = ["field0": "someId", "field1": "abcd"]
realm.create(Person.self, value: data, update: .error)
But, that will not “throw”, it will crash, so the code itself needs to be fixed. And really though, let’s do type safe calls as much as possible instead of relying on quoted strings.
So, in a nutshell, Do-Catch doesn’t Do-Catch with Realm.