Error Handling in Swift - Part 1

Well, a real positive here - I managed to back into my Xcode F1 Facts project and fix it! I had added an extra outlet to the button, which I removed. I couldn't get the tint colour to work, but that's no biggie. Also, on a really positive note, I got my iPhone connected! This means that I can actually show the App on my phone! This is a major breakthrough and a real achievement! Moving on, I'm going to start 'Error Handling' next. I've realised that there are several more modules in the Beginning Swift track - after Error Handling (which is only 1 hour - by far the shortest), I have building a vending machine app, auto layout basics and programmatic auto layout - none of which are that long. It means I have a bit more to do before parting ways with Treehouse for a bit to tackle the Udemy courses I've paid for. I will put the payment on pause while I do those, obviously. So, moving on with Error Handling....

Error Handling in Swift

So the idea is to look at the built in language structures of Swift. Pasan makes the point that learning about errors is essential. An error is a code that produces an unexpected result, or makes the program behaviour in an unintended manner.

Domain Errors - these are the simplest. E.g. converting between strings and numbers. Not having numbers within the string to convert into integers. In such cases, optionals are used. Using a failable init, the code can be handled basically.

Compiler Errors - Domain can be considered as compiler in some cases. Some errors - run-time errors - only happen when the actual code is run. You can have recoverable/unrecoverable types of errors.

Modelling Errors

Pasan shows the example of optional chaining to see if 'friend' can be returned safely. He shows the enum for error, which is built in to Swift. Not too sure about all this at the moment..
Challenge - easy, all complete

Throwing Errors

'throws' is used as the keyword within the function, before the return arrow; 'throw' is then used as part of the let guard statement. Complicated syntax. If you hit the throw statement, we exit the function completely, so that deals with the error I guess.

Next the 'try' keyword, with from: - again, all very tricky syntax. Not sure about any of this at the moment.

OK, that will do for now. To be honest, I'm a bit lost with this but I'm sure there'll be the chance to come back to it. It's not a long course, so I'm more keen to get stuck in with using Xcode projects again. 

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)