1

I'm trying to create and return a Storyboard programmatically while preventing a crash checking for an exception per Apple's documentation on UIStoryboard.

This method should return an exception if the storyboard mentioned doesn't exist:

init(name name: String,
   bundle storyboardBundleOrNil: NSBundle?)

As it stands putting the following code:

do {
    let storyboard = try UIStoryboard(name: "tester", bundle: nil)
} catch {
    // Do something
}

in a do-catch clause is being met by the pre-compiler with the following warning that the method:

'catch' block is unreachable because no errors are thrown in 'do' block

Might this be a bug?

Edit: 2015/11/19 After sending a radar to Apple about it, they have responded with the fact the issue is known and that my report is a duplicate of an open one.

Tokuriku
  • 1,292
  • 13
  • 22

1 Answers1

-1

The UIStoryboard initializer is not marked with throws hence you don't need to put it in a do-try-catch block

init(name name: String,
   bundle storyboardBundleOrNil: NSBundle?)

is enough then to change your code to

let storyboard = UIStoryboard(name: "tester", bundle: nil)

Notice that do-try-catch only catches errors and not NSException

You could read more about it here

If you want still to try to catch the NSException in that case you could see this answer: Catching NSException in Swift

Community
  • 1
  • 1
user4478383
  • 308
  • 2
  • 11
  • It's do catch, not try catch, and you don't need the try if it doesn't throw (in the second code example) – Charles A. Nov 06 '15 at 19:35
  • Directly from the documentation: Return Value: A storyboard object for the specified file. If no storyboard resource file matching name exists, an exception is thrown with description: Could not find a storyboard named 'XXXXXX' in bundle.... It's supposed to throw.. So my question is, is this a bug or a problem in documentation... An app WILL crash if the name is faulty btw. – Tokuriku Nov 06 '15 at 19:39
  • @Tokuriku https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html And you are using do-try-catch for ERROR handling, no exception handling in Swift – user4478383 Nov 06 '15 at 19:52
  • Please try and answer the question – Tokuriku Nov 06 '15 at 19:53
  • @Tokuriku yes I know perfectly that the app crashes. Anyway from iOS Developer Library: NOTE Error handling in Swift resembles exception handling in other languages, with the use of the try, catch and throw keywords. – user4478383 Nov 06 '15 at 20:05
  • You won't catch NSExceptions in Swift – user4478383 Nov 06 '15 at 20:47
  • If you want then to catch NSException you could try something like here: http://stackoverflow.com/questions/32758811/catching-nsexception-in-swift – user4478383 Nov 06 '15 at 20:56