[swift-evolution] A unified error handling mechanism?

Anders Ha hello at andersio.co
Fri Aug 5 23:22:38 CDT 2016


> On 6 Aug 2016, at 7:50 AM, Fernando Rodríguez via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi,
> 
> I do a lot of training and one of the features of Swift that seems more confusing to students (and myself) is error handling. There are too many ways of doing it, and none seems satisfactory.
> 
> Let's take for example an initializer. There are 2 different ways of handling errors within an init:
> 
> a) Old School: return nil. This was very simple in Objective C, because the uncommon case of an error could be easily ignored...until everything crashed.
> 
> In Swift it's not easy (nor advisable IMHO) to completely ignore the possibility of an error. Besides, it has 2 complications.
> 
> First of all, you return an Optional, and Optionals have a tendency to go viral. Suddenly, all your code has to deal with optionals.
> 
> Secondly, you have no information about the error itself.
> 
> b) do/try/catch
> 
> This allows you to have information about the error, but also causes the newly created object to be "trapped" inside a do block.

You can define an uninitialised variable outside the scope of `do`, and assign to it within the scope of `do`.


func test() throws -> Int { return 1 }

let variable: Int

do {
	variable = try test()
} catch let error {
	fatalError("\(error)")
}

print(variable)

> 
> Are there any plans to address this situation? I believe there should be a single, obvious and convenient way of handling errors in the language.
> 
> What do you guys think?
> 
> 
> 
> -- 
> 
> 
> 
>  <http://keepcoding.io/es/>
> 
> Fernando Rodríguez
> m. +34 610 965 332
> t. +34 91 629 57 61
> fernando at k <mailto:fernando at agbo.biz>eepcoding.io <http://eepcoding.io/>
>   
> KeepCoding.io
> 2120 University Avenue, Berkeley, CA
> 
> Avda. Fuencarral, 44, Ed. 8, Loft 30
> 28108 Alcobendas (Madrid) Spain
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160806/39fe621a/attachment.html>


More information about the swift-evolution mailing list