try? और try!
try? चुपचाप एक possible failure को crash होने के बजाय nil में बदल देता है, जबकि try! सब कुछ दांव पर लगाता है कि यह succeed होगा, न होने पर बुरी तरह crash करते हुए।
Syntax
let optional = try? throwingFunction() // nil if it throws
let value = try! throwingFunction() // crashes if it throws
Using try? to Get an Optional
try? एक throwing function के result को एक optional में convert करता है: success पर एक असली value, या अगर यह throw करे तो nil।
उदाहरण: Using try? to Get an Optional
// Define an enum `ParseError` listing its possible cases
enum ParseError: Error {
// Case `invalidFormat`
case invalidFormat
}
func parseNumber(_ text: String) throws -> Int {
// Unwrap `Int(text)` into `number`, or bail out via the else branch
guard let number = Int(text) else {
throw ParseError.invalidFormat
}
// Return `number` from this function
return number
}
// Declare a constant `good`, set to `try? parseNumber("42")`
let good = try? parseNumber("42")
// Declare a constant `bad`, set to `try? parseNumber("abc")`
let bad = try? parseNumber("abc")
// Print `good as Any` to the console
print(good as Any)
// Print `bad as Any` to the console
print(bad as Any)
Login to try C/C++/Java/PHP code in the editor
Using try! When Failure Is Impossible
try! सिर्फ तब उपयोग किया जाना चाहिए जब आप पूरी तरह निश्चित हों कि call fail नहीं हो सकती, क्योंकि कोई भी thrown error एक तुरंत crash बन जाती है।
Note:
अगर यहां string एक valid number नहीं होती,
try! तुरंत program को crash कर देता -- इसे sparingly उपयोग करें।उदाहरण: Using try! When Failure Is Impossible
// Define an enum `ParseError` listing its possible cases
enum ParseError: Error {
// Case `invalidFormat`
case invalidFormat
}
func parseNumber(_ text: String) throws -> Int {
// Unwrap `Int(text)` into `number`, or bail out via the else branch
guard let number = Int(text) else {
throw ParseError.invalidFormat
}
// Return `number` from this function
return number
}
// Declare a constant `number`, set to `try! parseNumber("100")`
let number = try! parseNumber("100")
// Print "Parsed with try!: \(number)" to the console
print("Parsed with try!: \(number)")
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
try!को एक ऐसी call पर उपयोग करना जो realistically fail हो सकती है; एक genuine failure पूरे program को बिना किसी recovery के crash कर देगी।- यह भूल जाना कि
try?पूरे expression को एक optional में बदल देता है, इसलिए एक successful call को अब भी unwrapping चाहिए। - उन errors को चुपचाप निगलने के लिए
try?overuse करना जिन्हें असल में inspect और handle किया जाना चाहिए, valuable failure information खोते हुए।
चैप्टर सारांश
try?एक throwing call के result को एक optional में convert करता है, किसी भी error कोnilमें बदलते हुए।try!एक throwing call को force-run करता है, असल में throw होने पर program को crash करते हुए।try?तब उपयोगी है जब आपको सिर्फ परवाह हो कि कुछ succeed हुआ या नहीं, यह क्यों fail हुआ नहीं।try!उन cases के लिए reserved रखा जाना चाहिए जहां failure सच में असंभव या एक genuine programming error हो।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: