कई Error Types को Handle करना
कभी-कभी किसी program के अलग-अलग हिस्से अलग-अलग तरीकों से fail हो सकते हैं, इसलिए आपको हर specific तरह की trouble पकड़ने के लिए एक plan चाहिए।
Syntax
do {
try throwingCall()
} catch ErrorTypeA.caseName {
// handle A
} catch ErrorTypeB {
// handle B
} catch {
// any other error, available as `error`
}
Catching Different Error Types Separately
कई catch clauses हर एक उसी do block के अलग-अलग हिस्सों से thrown एक distinct error type target कर सकते हैं।
उदाहरण: Catching Different Error Types Separately
// Define an enum `NetworkError` listing its possible cases
enum NetworkError: Error {
// Case `timeout`
case timeout
}
// Define an enum `ParsingError` listing its possible cases
enum ParsingError: Error {
// Case `malformed`
case malformed
}
func fetchData(shouldTimeout: Bool) throws {
// Check whether `shouldTimeout`
if shouldTimeout {
throw NetworkError.timeout
}
throw ParsingError.malformed
}
do {
try fetchData(shouldTimeout: true)
} catch is NetworkError {
// Print "A network error occurred" to the console
print("A network error occurred")
} catch is ParsingError {
// Print "A parsing error occurred" to the console
print("A parsing error occurred")
} catch {
// Print "Some other error" to the console
print("Some other error")
}
Login to try C/C++/Java/PHP code in the editor
Inspecting a Generic Caught Error
एक plain catch thrown value को Error type के error के रूप में bind करता है, जिसे ज़्यादा detail चाहिए होने पर एक specific type में cast किया जा सकता है।
उदाहरण: Inspecting a Generic Caught Error
// Define an enum `AppError` listing its possible cases
enum AppError: Error {
// Case `notFound`
case notFound(id: Int)
}
func lookup(id: Int) throws {
throw AppError.notFound(id: id)
}
do {
try lookup(id: 42)
} catch let appError as AppError {
// Print "App error: \(appError)" to the console
print("App error: \(appError)")
} catch {
// Print "Unhandled error: \(error)" to the console
print("Unhandled error: \(error)")
}
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. #}
आम गलतियां
- उसी single
catchclause में दो पूरी तरह अलग error enum typescatchकरने की कोशिश करना बिना हर type को अलग से pattern match किए। - यह भूल जाना कि एक generic
catch(बिना किसी type के) एक plainErrorvalue bind करता है, जिसे फिर एक specific concrete error type जांचने के लिए cast चाहिए। - Related error cases को एक shared enum में centralize न करना जब कोई function कई closely related तरीकों से fail हो सकता है, complexity को कई ad hoc error types में फैलाते हुए।
चैप्टर सारांश
- अलग throwing functions अपने खुद के distinct
Error-conforming types define और throw कर सकते हैं। - एक
doblock में कईcatchclauses हो सकते हैं, हर एक एक specific error type या case match करते हुए। - एक generic
catcherror को generalErrortype के रूप में bind करता है, जिसे आगे inspect करने के लिए casting चाहिए हो सकती है। - Related failure cases को एक enum में group करना किसी subsystem के लिए error handling organized रखता है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: