इसे समझने के लिए कुछ संदर्भों की खोज करने के बाद, दुर्भाग्य से- मुझे throws
और rethrows
के बीच अंतर को समझने के बारे में उपयोगी और सरल-वर्णन नहीं मिला। जब यह समझने की कोशिश की जाती है कि हमें उनका उपयोग कैसे करना चाहिए, यह भ्रमित है।स्विफ्ट में फेंकने और पुनर्विचार के बीच अंतर क्या हैं?
मैं उल्लेख है कि मैं, एक तरह से -default- throws
एक त्रुटि प्रचार के लिए इसके सरलतम रूप के साथ से परिचित हूँ इस प्रकार है:
enum CustomError: Error {
case potato
case tomato
}
func throwCustomError(_ string: String) throws {
if string.lowercased().trimmingCharacters(in: .whitespaces) == "potato" {
throw CustomError.potato
}
if string.lowercased().trimmingCharacters(in: .whitespaces) == "tomato" {
throw CustomError.tomato
}
}
do {
try throwCustomError("potato")
} catch let error as CustomError {
switch error {
case .potato:
print("potatos catched") // potatos catched
case .tomato:
print("tomato catched")
}
}
अब तक तो अच्छा है, लेकिन समस्या पैदा होती है जब:
func throwCustomError(function:(String) throws ->()) throws {
try function("throws string")
}
func rethrowCustomError(function:(String) throws ->()) rethrows {
try function("rethrows string")
}
rethrowCustomError { string in
print(string) // rethrows string
}
try throwCustomError { string in
print(string) // throws string
}
मैं अब तक क्या पता है जब एक समारोह है कि throws
यह rethrows
के विपरीत, एक try
द्वारा नियंत्रित किया जाना है बुला रहा है। तो क्या?! तर्क क्या है कि हमें throws
या rethrows
का उपयोग करने का निर्णय लेने पर पालन करना चाहिए?
ग्रेट उत्तर। धन्यवाद। – Darko
आखिरी वाक्य सुनहरा है! – Klaas
तो मुझे लगता है कि इसे समेटने के लिए, 'रीथ्रो' करें जब आप * फेंकना चाहते हैं। 'फेंक' जब आप ** हमेशा ** ** – Honey