Ruby begin/rescue/ ద్వారా లోపాలను నిర్వహిస్తుంది (Ruby యొక్క try/catch/finally సమానమైనది). మీరు అపవాదులను చేసి చేయండి, శుద్ధీకరణ కోసం ఉంటుంది. Ruby విధि-స్థర సంపాదన (స్పష్టమైన లేకుండా) కూడా అనుమతిస్తుంది, చిన్న కోడ్ కోసం.
Ruby begin/rescue/ ద్వారా లోపాలను నిర్వహిస్తుంది (Ruby యొక్క try/catch/finally సమానమైనది). మీరు అపవాదులను చేసి చేయండి, శుద్ధీకరణ కోసం ఉంటుంది. Ruby విధि-స్థర సంపాదన (స్పష్టమైన లేకుండా) కూడా అనుమతిస్తుంది, చిన్న కోడ్ కోసం.
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. నిర్దిష్ట రకాలను Rescue చేయండి (rescue ArgumentError), నిర్ధారిత శుద్ధీకరణ కోసం ensure ఉపయోగించండి, మరియు else లోపం లేని సందర్భం కోసం. retry బ్లాక్ను మళ్లీ ప్రయత్నించవచ్చు.
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
ముఖ్యమైనది: ఒక బేర్ rescue StandardError (అప్లికేషన్ లోపాల కోసం సరైన డిఫాల్ట్) ను క్యాచ్ చేస్తుంది. Exception (రూట్) ను Rescuing చేస్తుంది సమస్త యాంగ్ క్యాచ్ చేస్తుంది, Interrupt (Ctrl+C) మరియు SystemExit వంటి సిస్టమ్-స్థర సిగ్నల్లతో సహా — ఇది సాధారణంగా మీరు క్యాచ్ చేయవలసిన లేనిది, ఎందుకంటే ఇది ప్రోగ్రామ్ను అంతరాయం చేయకుండా లేదా సరిగా నిష్క్రమించకుండా నిరోధించవచ్చు. సాధారణ 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
సరైన అపవాద నిర్వహణ బలమైన Ruby అప్లికేషన్ల కోసం అవసరమైనది, మరియు Ruby యొక్క విధానాన్ని అర్థం చేసుకోవడం — దాని నిర్దిష్ట మూర్ఖతలు మరియు హెచ్చరికలతో సహా — ముఖ్యమైనది.
begin/rescue/ensure నిర్మాణం (Ruby యొక్క try/catch/finally), నిర్దిష్ట అపవాద రకాలను Rescuing చేయడం, నిర్ధారిత శుద్ధీకరణ కోసం ensure ఉపయోగించడం, మరియు చిన్న విధి-స్థర rescue (స్పష్టమైన begin లేకుండా) ఆధారమైన ప్రతిదిన జ్ఞానం.
ఒక ప్రత్యేకంగా ముఖ్యమైన మరియు విభిన్నంగా Ruby పాయింట్ StandardError vs Exception వ్యత్యాసం: ఒక బేర్ rescue (మరియు సరైన డిఫాల్ట్) StandardError ను క్యాచ్ చేస్తుంది, కానీ Exception (క్రమానికి రూట్) ను Rescuing చేస్తుంది సమస్త — Interrupt (Ctrl+C) మరియు SystemExit వంటి సిస్టమ్ సిగ్నల్లతో సహా — ఇది మీరు దాదాపుగా ఎప్పుడూ కోరుకోరు, ఎందుకంటే ఇది ప్రోగ్రామ్ను అంతరాయం చేయకుండా లేదా సరిగ్గా నిష్క్రమించకుండా నిరోధించవచ్చు, మరియు ఇది సాధారణ, హానికరమైన తప్పు.
మీరు అప్లికేషన్ లోపాల కోసం StandardError (Exception కాకుండా) ను Rescue చేయాలని అర్థం చేసుకోవడం ఫలితాలు మరియు సరైన బగ్లను నిరోధించే ముఖ్యమైన Ruby-నిర్దిష్ట జ్ఞానం.
నిర్మాణం, కస్టమ్ అపవాద తరగతులు (StandardError నుండి వారసత్వం), retry, విధి-స్థర rescue మూర్ఖత, మరియు ప్రత్యేకంగా StandardError-not-Exception హెచ్చరిక తెలుసుకోవడం బలమైన, సరైన Ruby అందించడానికి ముఖ్యమైనది, అపవాదులను దయగా నిర్వహిస్తుంది ప్రోగ్రామ్ కోసం అంతరాయం చేసే సామర్థ్యం ఉందంటే.
నిర్వహణ చేయని లోపాలు మరియు Exception-rescuing తప్పు రెండూ నిజమైన సమస్యలను కలిగిస్తాయి కాబట్టి, Ruby యొక్క అపవాద నిర్వహణని నిపుణత చేసుకోవడం — దాని నిర్మాణం, మూర్ఖతలు, మరియు విమర్శనీయమైన StandardError vs Exception వ్యత్యాసం — విశ్వసనీయ Ruby అభివృద్ధి మరియు సాధారణ సంభాషణ విషయం కోసం ముఖ్యమైన, తరచుగా-సంబంధితమైన జ్ఞానం.