Classification: Public
Hello,
I stumbled upon the exception handling and the quite practical guard
keyword for early-exiting a function.
Unfortunately, the try? discards an exception, if there is one.
What I am interested in is to use the shorter guard-style instead of the
lengthy do/try/catch-approach, example follows:
Current situation is (example #1):
let smth: AnyObject?
do {
smth = try myThrowingFunc()
} catch let error {
print(error) // here I can access the exception, but this code is
quite big and not very intuitive, and the result will always be an
optional
return
}
I would like to use it like this(example #2):
guard let smth = try? myThrowingFunc() else {
print(error) // in swift 2.2 the exception is discarded, so no access
possible, but this code is much cleaner
return
}
Advantages are better readability because of shorter code, and to avoid
the optionals from ex.#1 and therefore alot of ! and ? afterwards. In ex#2
I either have the smth constant, or the function stops and returns. In
ex#1, I will have an optional with a value, which again needs a few
additional steps to unwrap and use.
So I would love the ability to access exceptions (maybe with a standard
variable name) inside a guard-clause
I have created a question on stackoverflow here:
But have answered that already myself. I would love to get some feedback
from you guys, maybe you have some other tips for me.
Kind regards,
Matis Schotte
···
____________________________________________________
Matis Schotte
Mobile Solutions | Software Engineer
Deutsche Bank AG
Theodor-Heuss-Allee 72, 60486 Frankfurt am Main, Germany
Email matis.schotte@db.com
--
Informationen (einschließlich Pflichtangaben) zu einzelnen, innerhalb der EU tätigen Gesellschaften und Zweigniederlassungen des Konzerns Deutsche Bank finden Sie unter http://www.deutsche-bank.de/de/content/pflichtangaben.htm\. Diese E-Mail enthält vertrauliche und/ oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser E-Mail ist nicht gestattet.
Please refer to http://www.db.com/en/content/eu_disclosures.htm for information (including mandatory corporate particulars) on selected Deutsche Bank branches and group companies registered or incorporated in the European Union. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.