Ruby begin/rescue/ (Ruby کا try/catch/finally کے برابر) سے errors کو handle کرتا ہے۔ آپ exceptions کو کرتے ہیں اور کرتے ہیں، اور cleanup کے لیے استعمال ہوتا ہے۔ Ruby method-level rescue بھی کی اجازت دیتا ہے (explicit کے بغیر) صاف کوڈ کے لیے۔
Ruby begin/rescue/ (Ruby کا try/catch/finally کے برابر) سے errors کو handle کرتا ہے۔ آپ exceptions کو کرتے ہیں اور کرتے ہیں، اور cleanup کے لیے استعمال ہوتا ہے۔ Ruby 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 ہے۔ مخصوص قسمیں rescue کریں (rescue ArgumentError)، cleanup کے لیے ensure استعمال کریں، اور no-error کی صورت میں 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 کو پکڑتا ہے (application errors کے لیے صحیح ڈیفالٹ)۔ Exception کو rescue کرنا (جڑ) سب کچھ پکڑتا ہے، بشمول system-level signals جیسے Interrupt (Ctrl-C) اور SystemExit — جو عام طور پر آپ نہیں پکڑنا چاہتے، کیونکہ یہ program کو منقطع ہونے یا صحیح طریقے سے بند ہونے سے روک سکتا ہے۔ یہ 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 کے approach کو سمجھنا — اس کے مخصوص idioms اور احتیاطات کو شامل کرتے ہوئے — اہم ہے۔
begin/rescue/ensure ڈھانچہ (Ruby کا try/catch/finally)، مخصوص exception اقسام کو rescue کرنا، cleanup کے لیے ensure استعمال کرنا، اور صاف method-level rescue (explicit begin کے بغیر) بنیادی روزمرہ کی معلومات ہیں۔
ایک خاص اور منفرد Ruby نکتہ StandardError بمقابلہ Exception کا فرق ہے: ایک سادہ rescue (اور صحیح default) StandardError کو پکڑتا ہے، لیکن Exception (hierarchy کی جڑ) کو rescue کرنا سب کچھ پکڑتا ہے — بشمول system signals جیسے Interrupt (Ctrl-C) اور SystemExit — جو آپ تقریباً کبھی نہیں چاہتے، کیونکہ یہ program کو منقطع ہونے یا صحیح طریقے سے بند ہونے سے روک سکتا ہے، اور یہ ایک عام، نقصان دہ غلطی ہے۔
یہ سمجھنا کہ آپ application errors کے لیے StandardError کو (نہ کہ Exception) rescue کریں اہم Ruby-specific معلومات ہے جو اصل bugs سے بچاتی ہے۔
ڈھانچے، custom exception classes (جو StandardError سے وراثت میں لیں)، retry، method-level rescue idiom، اور خاص طور پر StandardError-not-Exception کی احتیاط کو جاننا مضبوط، درست Ruby لکھنے کے لیے اہم ہے جو errors کو gracefully handle کرے program کی منقطع ہونے یا بند ہونے کی صلاحیت کو توڑے بغیر۔
چونکہ unhandled errors اور Exception-rescuing کی غلطی دونوں حقیقی مسائل کا سبب بنتی ہیں، Ruby کی exception handling میں مہارت حاصل کرنا — اس کا ڈھانچہ، idioms، اور اہم StandardError بمقابلہ Exception کا فرق — قابل اعتماد Ruby development کے لیے اہم، بار بار متعلقہ معلومات ہے اور ایک عام انٹرویو topic ہے۔
تفصیلی جوابات کے ساتھ IT انٹرویو سوالات کی ایک لائبریری — جونیئر سے سینئر تک۔
عطیہ دیں