Ruby begin/rescue/ (Ruby's equivalent of try/catch/finally) सह errors हँडल करते. तुम exceptions करता आणि करता, cleanup साठी. Ruby method-level rescue (explicit शिवाय) देखील allow करते cleaner code साठी.
Ruby begin/rescue/ (Ruby's equivalent of try/catch/finally) सह errors हँडल करते. तुम exceptions करता आणि करता, cleanup साठी. Ruby method-level rescue (explicit शिवाय) देखील allow करते cleaner code साठी.
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's try/catch/finally आहे. विशिष्ट प्रकार rescue करा (rescue ArgumentError), guaranteed cleanup साठी ensure वापरा, आणि no-error case साठी else वापरा. retry block पुन्हा attempt करू शकते.
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 करते, system-level signals जसे Interrupt (Ctrl-C) आणि SystemExit यांचा समावेश - ज्यांना तुम्ही सामान्यतः 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 robust Ruby applications साठी आवश्यक आहे, आणि Ruby's approach समजून घेणे - त्याच्या विशिष्ट idioms आणि cautions समावेश - महत्वाचे आहे.
begin/rescue/ensure structure (Ruby's try/catch/finally), विशिष्ट exception types rescue करणे, guaranteed cleanup साठी ensure वापरणे, आणि clean method-level rescue (explicit begin शिवाय) हे मूलभूत दैनंदिन ज्ञान आहे.
विशेषतः महत्वाचा आणि विशिष्टतः Ruby बिंदू हा StandardError विरुद्ध Exception भेद आहे: एक bare rescue (आणि योग्य default) StandardError catch करते, परंतु Exception (hierarchy root) rescue करणे सर्वकाही catch करते - system signals जसे Interrupt (Ctrl-C) आणि SystemExit यांचा समावेश - ज्यांना तुम्ही जवळपास कधीच करू इच्छित नाही, कारण ते program interrupt किंवा exit होण्यास प्रतिबंध करू शकते, आणि हा एक सामान्य, हानिकारक चूक आहे.
हे समजून घेणे की तुम्ही application errors साठी StandardError (not Exception) rescue केले पाहिजे हे महत्वाचे Ruby-specific ज्ञान आहे जे real bugs प्रतिबंध करते.
Structure, custom exception classes (StandardError पासून inherit), retry, method-level rescue idiom, आणि विशेषतः StandardError-not-Exception caution जाणून घेणे robust, correct Ruby लिहिण्यासाठी महत्वाचे आहे जो errors gracefully हँडल करते program's ability interrupt न करता. Unhandled errors आणि Exception-rescuing mistake दोन्ही real problems cause करते, Ruby's exception handling मास्टर करणे - त्याचे structure, idioms, आणि critical StandardError विरुद्ध Exception भेद - reliable Ruby development आणि सामान्य interview topic साठी महत्वाचे, वारंवार relevant ज्ञान आहे.