TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Mistake in question OR I don't get it :)

Forums > 100 Days of SwiftUI

Hello,

There seems to be a mistake for the following question: https://www.hackingwithswift.com/review/sixty/optional-try

Optional try Question 2/6: Which of these are true about throwing functions?

Option 1: If you use try and know your function won't throw errors, you don't need to catch anything. Swift will force you to catch all errors.

Option 2: If you use try! and your call throws an error, your code crashes. This is correct.

But actually this code works:

enum MyError: Error {
    case critical
}

func returnString(_ toggle: Bool) throws -> String {
    if toggle {
        return "text"
    } else {
        throw MyError.critical
    }
}

var myVar = try returnString(true)
print(myVar)

It prints: text

Is there something I'm missing here?

1      

A Boolean has only two possible values - true and false.

You are passing true as the value into the function, so it returns text. Try passing false instead.

Here is an expansion of your code. I have introduced another case in the enum - critical2 & medium. Play around with the values - trying the original 5, then try it replacing the value with 11. See what happens.

enum MyError: Error {
    case critical
    case critical2
    case medium
}

func returnString(_ toggle: Bool) throws -> String {
    if toggle {
        return "text"
    } else {
        throw MyError.critical
    }
}

var myVar = try returnString(true)
print(myVar)

func returnBool( _ aValue: Int) throws -> Bool {
    if aValue > 10 {
        return true
    } else if aValue < 0 {
        throw MyError.medium
    } else {
        throw MyError.critical2
    }
}

var myBool = try returnBool(5)    // replace the 5 with 11 or -1 and see the result
print(myBool)

1      

Hey Greenamberred,

I understood how it works, I am saying that the marked correct answer seems wrong -- and I gave an example to demonstrate it.

Option 1: If you use try and know your function won't throw errors, you don't need to catch anything. Swift will force you to catch all errors.

The bold text is the "correct answer" according to HWS.

1      

John's example needs a little clarification:

enum MyError: Error {
    case critical
}

// This is a function that performs a trivial task, when input is true.
// But it also is a throwing function.  When false, it throws an error.  
// Do I need to catch all the errors? Let's find out.
func returnString(_ someCondition: Bool) throws -> String {
    if someCondition {
        print("This function throws, but I dont catch all the errors.")
        return "did not catch possible errors"
    } else {
        throw MyError.critical  // throwing function requires code to Throw something
    }
}

// Calling a throwing function.
// Notice I do not catch any errors.
print(try returnString(true))  // CONSOLE -> did not catch possible errors

// @twoStraws says that Swift will force you to catch all errors.
// This code seems to show the opposite.

2      

Thanks @Obelix :), you really made it clearer -- that's what I wanted to say.

1      

Where do you execute this code? Is it a real project or on Playgrounds? In a real project you get the error 'Errors thrown from here are not handled' and the project doesn't compile. On the Playground the code executes but setting the variable to false doesn't.

2      

Hey,

In real project made for terminal (Command Line Terminal), not in playground. I am not even using Xcode as an IDE, but rather AppCode.

1      

Perhaps this is the reason. As I said Xcode doesn't compile without handling errors and this is the expected behaviour.

Perhaps AppCode sees the parameter you enter as a constant (true) and "knows" it can't fail. But that I don't know. Does it change if you use a parameter which can be randomly true or false?

1      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.