← Back to Swift Course | Chapter 11: Error Handling | Lesson 6 of 6

कई Error Types को Handle करना

कभी-कभी किसी program के अलग-अलग हिस्से अलग-अलग तरीकों से fail हो सकते हैं, इसलिए आपको हर specific तरह की trouble पकड़ने के लिए एक plan चाहिए।
Syntax
swift
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

markup
// 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")
}

Inspecting a Generic Caught Error

एक plain catch thrown value को Error type के error के रूप में bind करता है, जिसे ज़्यादा detail चाहिए होने पर एक specific type में cast किया जा सकता है।

उदाहरण: Inspecting a Generic Caught Error

markup
// 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)")
}
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. #}
आम गलतियां
  1. उसी single catch clause में दो पूरी तरह अलग error enum types catch करने की कोशिश करना बिना हर type को अलग से pattern match किए।
  2. यह भूल जाना कि एक generic catch (बिना किसी type के) एक plain Error value bind करता है, जिसे फिर एक specific concrete error type जांचने के लिए cast चाहिए।
  3. Related error cases को एक shared enum में centralize न करना जब कोई function कई closely related तरीकों से fail हो सकता है, complexity को कई ad hoc error types में फैलाते हुए।
चैप्टर सारांश
  • अलग throwing functions अपने खुद के distinct Error-conforming types define और throw कर सकते हैं।
  • एक do block में कई catch clauses हो सकते हैं, हर एक एक specific error type या case match करते हुए।
  • एक generic catch error को general Error type के रूप में 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.