Is deinit called when init throws an exception?

Viewed 431

Consider the following example class:

class SocketWrapper {
    let sock: Int32

    init() throws {
        try sock = SocketWrapper.createSocket()
    }

    deinit {
        close(sock)
    }
}

What happens if createSocket() throws and the init() therefore fails? sock would be left uninitialized. Is deinit still called (on an partially uninitialized object) when init() throws?

1 Answers
Related