Exception handling with fast

I have a question about exception handling in Swift. The UIKit documentation for the UIStoryboard class states that the instantiateViewControllerWithIdentifier (identifier: String) -> UIViewController function throws an exception if the identifier is zero or does not exist in the storyboard. However, if I use do / try / catch as shown below, I get the warning "There are no calls to metal functions in the try expression."

This is just a warning, so I thought it was a problem with intellisense; but when I run the following code and intentionally use an invalid identifier, an exception will not be detected and SIGABRT is thrown.

let storyboard = UIStoryboard.init(name: "Main", bundle: nil) do { let controller = try storyboard.instantiateViewControllerWithIdentifier("SearchPopup") // This code is only included for completeness... controller.modalPresentationStyle = .Popover if let secPopoverPresentationController = controller.popoverPresentationController { secPopoverPresentationController.sourceView = self.view secPopoverPresentationController.permittedArrowDirections = .Any secPopoverPresentationController.barButtonItem = self.bSearchButton } self.presentViewController(controller, animated: true, completion: nil) // End code included for completeness. } catch { NSLog( "Exception thrown instantiating view controller." ); return; } 

How should you do / try / catch for functions that throw such exceptions?

Thanks in advance.

Brian

+5
source share
3 answers

instantiateViewControllerWithIdentifier is not a throw function, and you cannot handle it using do ... try ... catch. If the view controller is not available in the storyboard, you cannot do anything. This is a programmer’s mistake, the one who created this should solve such problems. You cannot blame the iOS runtime for such errors.

+1
source

This is a specific case of the more general issue discussed in Sweeping NSException

Summary it seems that quick exceptions and objc exceptions are different.

In this case, the online documentation says that it throws an exception, but it is impossible to catch; which sounds like a documentation error, at least.

I disagree with the other answers here that the missing VC is clearly a programmer error. If the behavior has been documented, you can postulate a design in which the generic code reacts differently depending on whether VC is present or not in a particular case | product | localization. Additional configuration needs to be added to ensure that the VC boots only when it is present - this is an invitation to errors on the verge and the like. cf update anomalies.

+3
source

You are not recovering from this exception, this exception is a RuntimeException .
Just ask yourself: "How would I react to this?" - if the answer is “I don’t know,” then why do you want to catch it?

Take the wrong identifier example - what will you do if you catch an error?

You cannot restore in any way that makes sense. If the identifier you submit cannot be found, this is what the developer did wrong when creating the application. This is what would and should be obvious when testing the application. If you somehow miss this application, your application will fail in the Apple review or on the client device.

0
source

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


All Articles