Ruby errors को begin/rescue/ के साथ संभालता है (Ruby का try/catch/finally के समकक्ष)। आप exceptions को करते हैं और उन्हें करते हैं, cleanup के लिए के साथ। Ruby साफ़ code के लिए method-level rescue (explicit के बिना) की भी अनुमति देता है।
Ruby errors को begin/rescue/ के साथ संभालता है (Ruby का try/catch/finally के समकक्ष)। आप exceptions को करते हैं और उन्हें करते हैं, cleanup के लिए के साथ। Ruby साफ़ code के लिए method-level rescue (explicit के बिना) की भी अनुमति देता है।
ensureraiserescueensurebeginbegin
result = risky_operation
rescue ArgumentError => e # rescue a SPECIFIC exception type
puts "Bad argument: #{e.message}"
rescue StandardError => e # broader catch (StandardError, not Exception)
puts "Error: #{e.message}"
retry if attempts < 3 # retry can re-run the begin block
else
puts "succeeded" # runs if NO exception
ensure
cleanup # ALWAYS runs (success or failure)
end
begin/rescue/ensure Ruby का try/catch/finally है। विशिष्ट types को rescue करें (rescue ArgumentError), guaranteed cleanup के लिए ensure का उपयोग करें, और no-error मामले के लिए else। retry block को फिर से प्रयास कर सकता है।
rescue => e # ✅ bare rescue catches StandardError (the right default)
rescue StandardError # ✅ explicit, same thing
rescue Exception # ❌ AVOID — catches EVERYTHING including system signals
# (Interrupt/Ctrl-C, SystemExit) — can break the program
महत्वपूर्ण: एक bare rescue StandardError को catch करता है (application errors के लिए सही default)। Exception (root) को rescue करना सब कुछ catch करता है, जिसमें Interrupt (Ctrl-C) और SystemExit जैसे system-level signals शामिल हैं — जिन्हें आप आमतौर पर catch नहीं करना चाहते, क्योंकि यह program को interrupt होने या ठीक से exit होने से रोक सकता है। एक आम Ruby गलती।
raise ArgumentError, "Amount must be positive" # raise a built-in
raise "Something failed" # raises RuntimeError
# custom exception class
class InsufficientFundsError < StandardError # inherit from StandardError
def initialize(msg = "Not enough funds")
super
end
end
raise InsufficientFundsError
def process
do_work
rescue => e # rescue WITHOUT begin — applies to the whole method body
handle(e)
ensure
cleanup
end
उचित exception handling मजबूत Ruby applications के लिए आवश्यक है, और Ruby के दृष्टिकोण को समझना — इसके विशिष्ट idioms और caveats सहित — महत्वपूर्ण है।
begin/rescue/ensure संरचना (Ruby का try/catch/finally), विशिष्ट exception types को rescue करना, guaranteed cleanup के लिए ensure का उपयोग, और साफ़ method-level rescue (explicit begin के बिना) मूलभूत रोज़मर्रा का ज्ञान हैं।
एक विशेष रूप से महत्वपूर्ण और स्पष्ट रूप से Ruby बिंदु है StandardError बनाम Exception अंतर: एक bare rescue (और सही default) StandardError को catch करता है, लेकिन Exception (hierarchy का root) को rescue करना सब कुछ catch करता है — जिसमें Interrupt (Ctrl-C) और SystemExit जैसे system signals शामिल हैं — जिसे आप लगभग कभी नहीं चाहते, क्योंकि यह program को interrupt होने या ठीक से exit होने से रोक सकता है, और एक आम, हानिकारक गलती है।
यह समझना कि आपको application errors के लिए StandardError (न कि Exception) को rescue करना चाहिए, महत्वपूर्ण Ruby-विशिष्ट ज्ञान है जो वास्तविक bugs को रोकता है।
संरचना, custom exception classes (StandardError से inherit करते हुए), retry, method-level rescue idiom, और विशेष रूप से StandardError-न-कि-Exception caveat को जानना मजबूत, सही Ruby लिखने के लिए महत्वपूर्ण है जो program की interrupt होने की क्षमता को तोड़े बिना errors को सुंदर ढंग से संभालता है।
चूँकि unhandled errors और Exception-rescuing गलती दोनों वास्तविक समस्याएँ पैदा करते हैं, Ruby की exception handling पर महारत हासिल करना — इसकी संरचना, idioms, और महत्वपूर्ण StandardError बनाम Exception अंतर — विश्वसनीय Ruby विकास के लिए महत्वपूर्ण, बार-बार प्रासंगिक ज्ञान है और एक आम interview विषय है।