catch performSegue(withIdentifier)

performSegue(withIdentifier) documentation says "This method throws an Exception handling ..." (sic)

yet the function is not marked as "throws".


I know for a fact that it causes an exception in some cases.


I try to put it in a do/try/catch block and Xcode tells me

"No calls to throwing functions occur within the 'try' expression" and

"'Catch' block is unreachable because no errors are thrown in the 'do' block"


How do I trap this exception? Thanks.

It throws an Obj-C exception (NSException), which is a kind of app crash, not a Swift exception, which is a kind of error.


You can't handle NSExceptions in Swift, and (current best practice) you shouldn't try. It's generally correct to let the app crash.


Note that segue identifiers are static in a storyboard, so it shouldn't be necessary to test for the existence of a segue at run time.

If identifier is unknown, you get this log after crash:


2020-01-17 18:52:39.663553+0100 simpleTest[59418:17291338] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<simpleTest.Item1ViewController: 0x7fdbac627870>) has no segue with identifier 'ColorSegueX''


This is a programmer error, and should be corrected as such.

From this document:
https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/ExceptionHandling.html#//apple_ref/doc/uid/TP40008195-CH18
I get the impression that it probably is possible to catch these exceptions... but using ObjC stuff in Swift is beyond me at this point.


Yes, probably all exceptions are user errors. It would be nice to be able to catch it so some debug is possible.


I finally tracked this one down (I think). My mainVC keeps track of what other ViewController is currently on the screen. All of those (I thought) have the necessary segue identifier. What I didn't take into account was when the mainVC was presenting a UIAlertController! Needless to say, this doesn't have the segue identifier (and I wouldn't want to segue from there if I could).


Thanks for your replies. I really appreciate it!

Any ideas on my showBluetoothAccessoryPicker post?

Barry

Great you solved it and thanks for feedback.


Catching the error comes with the crash and the crashlog… Even with an exception, what would you tell to user ? "Sorry, there was an error in code" ?


If you want someone to have a look, please post the code where you call the segue and where you alert.


If you want some news about showBluetoothAccessoryPicker post, you should post its url.


Anyway, don't forget to close the thread.


EDITED

well, search for Bluetooth forum and fount your post.

Not surprise you did not get any answer, the question is badly incomplete.

catch performSegue(withIdentifier)
 
 
Q