Catch all errors, exceptions, no matter what it is, in Swift

I am new to Swift and I have a little problem.

I have a piece of code, and any line can potentially cause an error.

My problem is that I do not want to go line by line, catching every error, I want to catch them all in one expression.

In python you can do it

try: exampleArray = [1,2,3,4] print(exampleArray[4]) except Exception as e: print(e) pass 

What this means is an attempt to print a value from an array that does not exist, but it falls into the except statement, I wonder if something like this is possible in Swift

To clarify, I'm not trying to catch the index out of range error, I just want to catch the error, regardless of what it is.

Is this possible without announcing my own mistakes and throwing them line by line?

+5
source share
1 answer

In Swift, you can only catch errors that throw n.

Since not all errors are handled with throw ing (for example, accessing an array out of range), you cannot catch everything.

0
source

Source: https://habr.com/ru/post/1264400/


All Articles