Ruby errors ko begin/rescue/ (Ruby's equivalent of try/catch/finally) naal handle karta hai. Tusi karde ho exceptions aur unhe karde ho, cleanup ke liye. Ruby method-level rescue bhi allow karda hai (explicit bina) cleaner code ke liye.
Ruby errors ko begin/rescue/ (Ruby's equivalent of try/catch/finally) naal handle karta hai. Tusi karde ho exceptions aur unhe karde ho, cleanup ke liye. Ruby method-level rescue bhi allow karda hai (explicit bina) cleaner code ke liye.
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 hai. Specific types rescue karo (rescue ArgumentError), ensure guaranteed cleanup ke liye use karo, aur else no-error case ke liye. retry block ko dubara try kar sakda hai.
Exception nahi, StandardError rescue karorescue => 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
Important: ek bare rescue StandardError catch karda hai (application errors ke liye sahi default). Exception (root) rescue karna sab kuch catch karta hai, jis vich system-level signals jaise Interrupt (Ctrl-C) aur SystemExit bhi shamil hain — jo tusi usually catch nahi karna chahunde ho, kyunki program ko interrupt karne ya exit karne se rok sakda hai. Ruby da ek common mistake.
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
Mukhri exception handling robust Ruby applications ke liye zaroori hai, aur Ruby's approach samajhna — uske specific idioms aur caveats da — mahatvpoorn hai.
begin/rescue/ensure structure (Ruby's try/catch/finally), specific exception types rescue karna, ensure guaranteed cleanup ke liye, aur clean method-level rescue (bina explicit begin) fundamental everyday knowledge hain.
Ek particularly important aur distinctly Ruby point StandardError vs Exception distinction hai: ek bare rescue (aur sahi default) StandardError catch karda hai, par Exception (hierarchy da root) rescue karna sab kuch catch karda hai — jis vich system signals jaise Interrupt (Ctrl-C) aur SystemExit bhi shamil hain — jo tusi almost kabhi nahi chahunde, kyunki program nu interrupt ya exit karne naal rok sakda hai, aur ek common, harmful mistake hai.
Yeh samajhna ki tusi StandardError (Exception nahi) application errors ke liye rescue karo important Ruby-specific knowledge hai jo real bugs rokda hai.
Structure, custom exception classes (jinhaan StandardError inherit karda hai), retry, method-level rescue idiom, aur especially StandardError-not-Exception caveat jannu important Ruby exception handling liye likh ke robust, correct Ruby banata hai jo errors gracefully handle karda hai bina program's interrupt karne capability nu break kare.
Chunki unhandled errors aur Exception-rescuing mistake dono real problems karan, Ruby's exception handling — uska structure, idioms, aur critical StandardError vs Exception distinction — master karna important, frequently-relevant knowledge hai reliable Ruby development aur common interview topic ke liye.
ਵਿਸਤ੍ਰਿਤ ਜਵਾਬਾਂ ਨਾਲ IT ਇੰਟਰਵਿਊ ਸਵਾਲਾਂ ਦੀ ਇੱਕ ਲਾਇਬ੍ਰੇਰੀ — ਜੂਨੀਅਰ ਤੋਂ ਸੀਨੀਅਰ ਤੱਕ।
ਦਾਨ ਕਰੋ