ရိုးရိုးလေး ရှင်းပြမယ်
Exception ကို ရိုးရိုးလေး ပြောရရင်
Exception ဆိုတာ Ruby က "ပုံမှန်အတိုင်း ဆက်မလုပ်နိုင်တော့ဘူး" လို့ ပြောတဲ့ နည်းလမ်းပါ။ သုညနဲ့ စားတာ၊ မရှိတဲ့ file ကို ဖတ်တာလို ပြဿနာတစ်ခု ပေါ်တဲ့အခါ Ruby က exception object တစ်ခု ဖန်တီးပြီး ပြန်ထွက်ပါတယ်: လက်ရှိ method ကနေ ထွက်၊ အဲဒါကို ခေါ်ခဲ့တဲ့ method ကနေ ထွက်၊ ဒီလိုနဲ့ ဒီပြဿနာကို ကိုင်တွယ်နိုင်တဲ့ code ကို ရှာသွားပါတယ်။ begin ... rescue ကဲ့သို့သော code က အဲဒါပါ။ ဘယ်သူမှ မကိုင်တွယ်ရင် program ရပ်ပြီး message နဲ့ backtrace ကို print ထုတ်ပါတယ်။
ensure ကတော့ သန့်ရှင်းရေးအဖွဲ့ပါ: အလုပ်အောင်မြင်သည်ဖြစ်စေ၊ ပျက်ကွက်သည်ဖြစ်စေ၊ အစောပိုင်းမှာ return လုပ်သည်ဖြစ်စေ သူ့ code က run ပါတယ်။ ပျက်ကွက်မှုကို ဘာလုပ်မလဲ ဆုံးဖြတ်ဖို့ rescue ကို သုံးပြီး file ပိတ်တာ၊ lock လွှတ်တာလို အရာတွေကို နေရာမှန်ပြန်ထားဖို့ ensure ကို သုံးပါ။
Problem → Why → Solution
ပြဿနာ: error တစ်ခုက program တစ်ခုလုံးကို ရပ်စေတယ်
Script တစ်ခုက အသုံးပြုသူတွေ ရိုက်ထည့်တဲ့ ဈေးနှုန်းတွေကို ဖတ်ပြီး နှစ်ဆ တွက်ပါတယ်။ တစ်နေ့ ဘယ်သူ့ကမှ "12 USD" လို့ ရိုက်လိုက်ပါတယ်။ Integer("12 USD") က ArgumentError ကို raise လုပ်ပြီး ဘယ်သူမှ မကိုင်တွယ်တာမို့ program တစ်ခုလုံး အဲဒီလိုင်းမှာ ပြီးသွားပါတယ်၊ နောက်ကလိုင်းတွေ မ run ဘဲ တစ်ဝက်တစ်ပျက်ဖြစ်နေတဲ့ အလုပ်တွေက ပစ်ထားခံရပါတယ်။
ဖြေရှင်းချင်တဲ့ စိတ်က error အားလုံးကို rescue လုပ်ပြီး nil ပြန်တာ ဒါမှမဟုတ် rescue Exception လုပ်တာပါ။ ဒါက crash ကို ဖယ်ပေမယ့် အချက်အလက်ကိုပါ ဖယ်လိုက်ပါတယ်: ကိုယ့် code ထဲက စာလုံးပေါင်းမှားတာတောင် အသုံးပြုသူ input မှားတာလို့ မြင်ရပြီး ဘယ်သူမှ မမျှော်လင့်တဲ့ nil နဲ့ program ဆက်သွားပါတယ်။ ရည်ရွယ်ချက်က မျှော်လင့်ထားတဲ့ ပျက်ကွက်မှုတွေကို ဘာလုပ်ရမလဲ သိတဲ့နေရာမှာ ကိုင်တွယ်ပြီး ကျန်တာတွေကို ကျယ်ကျယ်လောင်လောင် ထားဖို့ပါ။
def load_price(text)
Integer(text) * 2
end
puts load_price("12")
puts load_price("12 USD") # ArgumentError ends the whole program here
puts "never printed"မျှော်လင့်ထားတာကို ကိုင်တွယ်ပြီး ကျန်တာကို ကျယ်ကျယ်လောင်လောင် ထားပါ
Exception တွေက တစ်ခုခုက rescue မလုပ်မချင်း call stack အတိုင်း အပေါ်ကို ခရီးသွားတာမို့ ပြဿနာကို နားလည်တဲ့ code က သတိထားမိတဲ့ code နဲ့ ဝေးနိုင်ပါတယ်။ သီးသန့် class ကို rescue လုပ်ရင် စီစဉ်ထားတဲ့ ပျက်ကွက်မှုတစ်ခုကို ပြန်လည်ကောင်းမွန်စေပြီး မမျှော်လင့်တာကတော့ message နဲ့ backtrace မပျက်စီးဘဲ အပေါ်ကို ရောက်ပါတယ်။ ensure က လမ်းကြောင်းတိုင်းမှာ clean-up code run ကြောင်း အာမခံပေးတဲ့ ကျန်တဲ့အပိုင်းပါ။
Core concept: raise, rescue, else နဲ့ ensure
raise က Exception ကနေ ဆင်းသက်လာတဲ့ class ရဲ့ instance ဖြစ်တဲ့ exception object တစ်ခုကို ဖန်တီးပြီး call stack ကို ပြန်ဖြေပါတယ်။ ထွက်ခဲ့တဲ့ method တစ်ခုချင်းစီကို ချက်ချင်း စွန့်ပစ်ပါတယ်။ ကိုက်ညီတဲ့ rescue clause တွေ့ရင် အဲဒီကနေ ဆက်လုပ်ပြီး program ရဲ့ထိပ်ကို ရောက်ရင် Ruby က message နဲ့ backtrace ကို print ထုတ်ကာ error status နဲ့ ထွက်ပါတယ်။ Object ထဲမှာ အသေးစိတ်တွေ ပါပါတယ်: e.message က လူဖတ်ဖို့ စာသား၊ e.class က ဘယ်လို ပြဿနာလဲ၊ e.backtrace က ဘယ်မှာ ဖြစ်တာလဲ ဆိုတာတွေပါ။
begin block တစ်ခုမှာ clause လေးခုအထိ ရှိနိုင်ပါတယ်။ Body က အရင် run ပါတယ်။ rescue က body က ကိုက်ညီတဲ့ exception ကို raise လုပ်မှသာ run ပါတယ်။ else က body က raise မလုပ်ဘဲ ပြီးမှသာ run ပြီး else ထဲမှာ raise လုပ်တဲ့ exception ကို တူညီတဲ့ begin ရဲ့ rescue clause တွေက မဖမ်းပါဘူး။ ensure ကတော့ body အောင်မြင်သည်ဖြစ်စေ၊ ပျက်သည်ဖြစ်စေ၊ return ကို ထိတွေ့သည်ဖြစ်စေ အမြဲ run လို့ clean-up လုပ်ဖို့ သင့်တော်ပါတယ်။ Method body ကလည်း begin မပါဘဲ rescue, else, ensure ကို တိုက်ရိုက်သုံးနိုင်ပြီး do...end block ကလည်း အတူတူ လုပ်နိုင်ပါတယ်။
Rescue clause တွေကို အပေါ်ကနေ အောက်ကို စမ်းပြီး ပထမဆုံး ကိုက်တာက အနိုင်ရလို့ သီးသန့် class တွေကို ယေဘုယျ class တွေရှေ့မှာ ထားပါ၊ ကျယ်ပြန့်တဲ့ StandardError ကို ရှေ့မှာ ထားရင် အောက်က clause ကို ဘယ်တော့မှ မရောက်တော့ပါဘူး။ class မပါတဲ့ rescue => e က StandardError ကို ဆိုလိုပြီး ပရိုဂရမ်တွေ ပုံမှန် ဖြစ်စေတတ်တဲ့ error တွေ ဖြစ်တဲ့ ArgumentError, TypeError, ZeroDivisionError, NoMethodError, KeyError, IOError, RuntimeError တို့ကို ဖုံးပါတယ်။ rescue Exception ကို ဘယ်တော့မှ မရေးပါနဲ့။ Exception ထဲမှာ exit က raise လုပ်တဲ့ SystemExit၊ Ctrl-C က raise လုပ်တဲ့ Interrupt နဲ့ NoMemoryError တို့ပါလို့ ဒါကို rescue လုပ်ရင် program ကို ရပ်လို့မရတော့တဲ့ အနေအထား ဖြစ်သွားနိုင်ပါတယ်။
raise မှာ ပုံစံ အမျိုးမျိုး ရှိပါတယ်။ raise "text" က RuntimeError ကို raise လုပ်ပါတယ်။ raise ArgumentError က အဲဒီ class ကို နာမည်ကို message အဖြစ်နဲ့ raise လုပ်ပြီး raise ArgumentError, "bad value" က ကိုယ့် message ကို ထည့်ပေးပါတယ်။ raise ArgumentError.new("bad value") ကတော့ ကိုယ်တိုင်ဖန်တီးထားတဲ့ object ကို ပို့တာပါ။ rescue clause ထဲက ကွင်းမပါတဲ့ raise က လက်ရှိကိုင်တွယ်နေတဲ့ exception ကို ပြန် raise လုပ်ပါတယ်။ တခြား exception ကို rescue လုပ်နေရင်း exception အသစ် raise လုပ်ရင် Ruby က အဟောင်းကို အသစ်ရဲ့ cause အဖြစ် အလိုအလျောက် မှတ်ပေးပြီး raise X, cause: other က ကိုယ်တိုင် သတ်မှတ်ပေးပါတယ်။
- raise က exception object ဖန်တီး
- Ruby က call stack အတိုင်း အပေါ်တက်
- ပထမ ကိုက်တဲ့ rescue က ကိုင်တွယ်
- ensure က လမ်းကြောင်းတိုင်းမှာ run
ပြဿနာက သူ့ကို ကိုင်တွယ်မယ့် rescue ကို မတွေ့မချင်း အပေါ်ကို တက်သွားပြီး ensure ကတော့ ဘယ်လမ်းကြောင်းကနေ ထွက်ထွက် run ပါတယ်။
အထွေထွေ ပုံစံ
begin
risky_work
rescue SpecificError, OtherError => error
handle(error) # runs only for these classes
rescue StandardError => error
log(error.message) # broader, so it comes last
else
on_success # only when nothing was raised
ensure
clean_up # always runs
end
raise CustomError, "message"rescue clause တွေကို သီးသန့်ကနေ ယေဘုယျ အစီအစဉ်ချပါ၊ => error က exception object ကို variable ထဲ ထည့်ပါတယ်။ else က အောင်မြင်တဲ့ အခါ၊ ensure က အမြဲ run ပါတယ်။ နောက်ဆုံးလိုင်းက raise ကို ကိုယ်ပိုင် class နဲ့ message နဲ့ သုံးပုံပါ။
Example: safe_divide မှာ rescue, else နဲ့ ensure
အောက်က safe_divide မှာ rescue clause နှစ်ခု၊ else နဲ့ ensure ပါတဲ့ begin တစ်ခု ရှိပါတယ်။ Output ထဲက လိုင်းတွေ ပေါ်တဲ့ အစီအစဉ်ကို ကြည့်ပါ: အောင်မြင်တဲ့ ခေါ်ဆိုမှုမှာ else လိုင်းက ensure လိုင်းရှေ့မှာ ပေါ်ပြီး ပျက်ကွက်တဲ့ ခေါ်ဆိုမှုတွေမှာ rescue clause က nil ပြန်တာတောင် ensure လိုင်း ပေါ်ပါသေးတယ်။
Output ထဲက "divided by 0" နဲ့ "String can't be coerced into Integer" ဆိုတဲ့ message စာသားတွေက Ruby 4.0.9 ရဲ့ output ပါ။ မကြာသေးတဲ့ Ruby ဗားရှင်းတွေမှာ ဒီစာသားတွေ တည်ငြိမ်ပေမယ့် message ကို ပြသဖို့ စာသားအဖြစ်သာ သဘောထားပြီး code ထဲမှာ ယှဉ်စစ်ဖို့ မသုံးပါနဲ့။
def safe_divide(a, b)
begin
result = a / b
rescue ZeroDivisionError => e
puts "error: #{e.message}"
return nil
rescue TypeError => e
puts "type problem: #{e.message}"
return nil
else
puts "ok: #{result}"
ensure
puts "finished #{a} / #{b.inspect}"
end
result
end
p safe_divide(10, 2)
p safe_divide(1, 0)
p safe_divide(4, "x")Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The risky line
result = a / bbegin body ထဲမှာ raise လုပ်နိုင်တာက ဒီလိုင်းတစ်ခုတည်းပါ။ Body ကို သေးသေးထားတာက rescue က ဘာကို ကာကွယ်နေလဲ ရှင်းလင်းစေပါတယ်။
- A specific rescue
rescue ZeroDivisionError => eZeroDivisionError ကိုပဲ ဖမ်းပါတယ်။ e.message က "divided by 0" ဖြစ်ပြီး အခြား error တွေ (ဥပမာ NoMethodError) က ဒီ clause ကို ဖြတ်ပြီး အပေါ်ကို ဆက်တက်သွားမှာပါ။
- return inside rescue
return nilRescue ထဲက return က method ကနေ ထွက်ပေမယ့် ensure ကတော့ ဘာပဲဖြစ်ဖြစ် run ပါသေးတယ်။ ဒါကြောင့် output မှာ "finished 1 / 0" လိုင်း ပေါ်တာပါ။
- The else clause
puts "ok: #{result}"else က exception မရှိမှသာ run ပါတယ်၊ ဒါကြောင့် အောင်မြင်ကြောင်း ပြောတာကို ဒီမှာ ထားပါတယ်။ result ကို body ထဲမှာ သတ်မှတ်တာမို့ else ထဲကနေ သုံးလို့ရပါတယ်။
- The ensure clause
puts "finished #{a} / #{b.inspect}"ensure က ခေါ်ဆိုမှုသုံးခုလုံးမှာ run ပါတယ်။ b.inspect ကို သုံးတာကြောင့် "x" က နှစ်ထပ်ကွင်းနဲ့ ပေါ်ပြီး ဘာ type လဲ မြင်ရတာမို့ log လိုင်းတွေအတွက် အသုံးဝင်ပါတယ်။
- A TypeError case
p safe_divide(4, "x")4 / "x" က TypeError ကို raise လုပ်ပြီး ဒုတိယ rescue clause က ကိုင်တွယ်ပါတယ်။ Clause တစ်ခုချင်းစီက မတူတဲ့ ပျက်ကွက်မှုတွေကို သီးခြားစီ ဖြေရှင်းပေးနိုင်တာ ဒီမှာ မြင်ရပါတယ်။
ok: 5
finished 10 / 2
5
error: divided by 0
finished 1 / 0
nil
type problem: String can't be coerced into Integer
finished 4 / "x"
nil- Body ကို run
- Exception raise လုပ်လား စစ်
- ကိုက်တဲ့ rescue ကို အပေါ်ကနေ အောက် ရှာ
- Exception မရှိရင် else ကို run
- ensure ကို အမြဲ run
Exception က rescue နဲ့ ကိုက်ရင်
rescue body runs, then ensure
Exception မရှိရင်
else runs, then ensure
rescue, else နဲ့ ensure ရဲ့ အစီအစဉ်
Clause တွေရဲ့ အစီအစဉ်ကို မှတ်ထားဖို့ အကောင်းဆုံးနည်းက print လိုင်းတွေနဲ့ ကိုယ်တိုင်ကြည့်တာပါ။ အောက်က trace မှာ A ကို အောင်မြင်စွာ ခေါ်ပြီး B ကို exception နဲ့ ခေါ်ပါတယ်။ A အတွက် body၊ else၊ ensure အစီအစဉ် ရပြီး B အတွက်တော့ body ပြီးအောင် မ run ဘဲ ရပ်သွားကာ rescue၊ ensure အစီအစဉ်ကို ရပါတယ်၊ else မ run ပါဘူး။ trace ကို method-level rescue ပုံစံနဲ့ ရေးထားတာမို့ begin မလိုပါဘူး။ ensure ထဲမှာ return မရေးပါနဲ့: အဲဒီ return က အပေါ်ကို တက်လာနေတဲ့ exception ကို တိတ်တိတ်လေး ဖျက်ပစ်ပါလိမ့်မယ်။
def trace(label, fail: false)
puts "#{label}: start"
raise IOError, "disk error" if fail
puts "#{label}: body finished"
rescue IOError
puts "#{label}: rescue"
else
puts "#{label}: else"
ensure
puts "#{label}: ensure"
end
trace("A") # start, body finished, else, ensure
trace("B", fail: true) # start, rescue, ensure
def early
return :from_body
ensure
puts "ensure still runs"
end
p early # prints the ensure line, then :from_bodyretry ကို ကန့်သတ်ပြီး သုံးပါ
retry က rescue clause ထဲကနေ begin block ရဲ့ အစကို ပြန်ခုန်ပြီး ထပ်လုပ်ပါတယ်။ Network timeout လို ခဏတာ ပျက်ကွက်မှုတွေအတွက် အသုံးဝင်ပါတယ်။ ဒါပေမယ့် အကြိမ်ရေကို ကန့်သတ်မထားရင် တကယ်ပျက်နေတဲ့ အရာက loop အဆုံးမရှိ ဖြစ်စေတာမို့ counter ထားပြီး ကန့်သတ်ချက်ပြည့်ရင် error ကို ပြန် raise လုပ်ပါ။ retry က ensure ကို run မလုပ်ဘဲ ခုန်တာမို့ ensure ဟာ begin block ကို နောက်ဆုံး ထွက်တဲ့အခါမှ တစ်ကြိမ်ပဲ run ပါတယ်။
attempts = 0
begin
attempts += 1
puts "attempt #{attempts}"
raise IOError, "temporary failure" if attempts < 3
puts "succeeded"
rescue IOError => e
if attempts < 3
puts " #{e.message}, retrying"
retry
end
puts "giving up"
endCustom error၊ re-raise နဲ့ cause
ကိုယ့် app ရဲ့ ပျက်ကွက်မှုတွေကို class တစ်ခုချင်းစီ နာမည်ပေးပါ။ class AppError < StandardError; end လို base class တစ်ခုထားပြီး ValidationError < AppError လို subclass တွေ ထပ်ထားရင် ခေါ်သူက rescue AppError နဲ့ အားလုံးကို၊ ဒါမှမဟုတ် သီးသန့်တစ်ခုကို ဖမ်းနိုင်ပါတယ်။ Message မပေးရင် message က class နာမည်ပါ။ Extra data ထည့်ချင်ရင် initialize ကို override လုပ်ပြီး super(message) ခေါ်ပါ။ rescue ထဲမှာ exception အသစ် raise လုပ်ရင် cause ကို Ruby က မှတ်ပေးပါတယ်၊ ကွင်းမပါတဲ့ raise ကတော့ လက်ရှိ exception ကို မူရင်းအတိုင်း ဆက် ပို့ပါတယ်။
class AppError < StandardError; end
class ValidationError < AppError
attr_reader :field
def initialize(field, message = "#{field} is invalid")
@field = field
super(message)
end
end
class ConfigError < AppError; end
begin
raise ValidationError.new(:email)
rescue AppError => e
puts "#{e.class} field=#{e.field} message=#{e.message}"
end
begin
raise ValidationError, :age
rescue AppError => e
puts "#{e.class} field=#{e.field} message=#{e.message}"
end
def load_port(text)
Integer(text)
rescue ArgumentError
raise ConfigError, "port must be a number"
end
begin
load_port("80x")
rescue ConfigError => e
puts e.message # port must be a number
puts e.cause.class # ArgumentError
puts e.cause.message # invalid value for Integer(): "80x"
end
begin
begin
raise "low-level failure"
rescue => error
puts "logging: #{error.message}"
raise # same exception continues upward
end
rescue => again
puts "outer sees: #{again.message}"
puts again.equal?(error) # true
endrescue Exception ကို ဘာကြောင့် မရေးသင့်လဲ
Exception ထိပ်ဆုံး class က လူအများ ထင်တာထက် ပိုကျယ်ပါတယ်။ exit ကိုယ်တိုင်က SystemExit ကို raise လုပ်တာမို့ rescue Exception က "program ကို ရပ်ပါ" ဆိုတဲ့ တောင်းဆိုမှုကိုပါ မျိုချပါတယ်။ အောက်ကပုံက hierarchy ကို ပြပြီး bare rescue က StandardError အောက်ကိုပဲ ဖုံးတယ်ဆိုတာ ပြပါတယ်။ NotImplementedError လို ScriptError လည်း bare rescue ရဲ့ အပြင်ဘက်မှာ ရှိပါတယ်။
A slice of the exception hierarchy
----------------------------------
Exception
+-- NoMemoryError
+-- ScriptError ------- NotImplementedError
+-- SignalException --- Interrupt (Ctrl-C)
+-- SystemExit (exit)
+-- StandardError <- a bare "rescue" catches from here down
+-- ArgumentError
+-- IOError
+-- IndexError ----- KeyError, StopIteration
+-- NameError ------ NoMethodError
+-- RuntimeError --- FrozenError
+-- TypeError
+-- ZeroDivisionErrorbegin
exit 2
rescue SystemExit => e
puts "exit raised SystemExit with status #{e.status}"
end
p SystemExit.ancestors.first(3) # [SystemExit, Exception, Object]
p ArgumentError.superclass # StandardError
kinds = [ZeroDivisionError, KeyError, NotImplementedError, Interrupt]
p kinds.map { |k| k.ancestors.include?(StandardError) } # [true, true, false, false]
begin
Integer("x")
rescue StandardError
puts "first clause wins"
rescue ArgumentError
puts "unreachable"
endလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Web framework တွေက exception ကို ပျက်ကွက်တဲ့ code နဲ့ တုံ့ပြန်တဲ့ code ကြား နယ်နိမိတ်အဖြစ် သုံးပါတယ်: controller တစ်ခုက မရှိတဲ့ record လို သိပြီးသား error ကို rescue လုပ်ပြီး 404 စာမျက်နှာ ပြောင်းပေးပြီး မမျှော်လင့်တဲ့ exception ကိုတော့ error tracker ဆီ ပို့ကာ ယေဘုယျ 500 အဖြစ် ပြပါတယ်။ Library တွေက ကိုယ်ပိုင် error hierarchy ကို သတ်မှတ်ပါတယ်၊ ဥပမာ gem တစ်ခုစီမှာ base class တစ်ခုနဲ့ သီးသန့် subclass တွေ ထားလို့ ခေါ်သူက clause တစ်ခုနဲ့ library တစ်ခုလုံးကို ဒါမှမဟုတ် ပျက်ကွက်မှုတစ်ခုတည်းကို ဖမ်းနိုင်ပါတယ်။
Background job နဲ့ API client တွေက exception ကို retry နဲ့ ပေါင်းသုံးပါတယ်: timeout၊ connection ငြင်းပယ်ခံရတာလို ခဏတာ ပြဿနာတွေကို နှောင့်နှေးမှုနဲ့ အကြိမ်အနည်းငယ် retry လုပ်ပြီး validation ပျက်တာလို ပြီးပြတ်တဲ့ ပြဿနာတွေကို ချက်ချင်း report လုပ်ပါတယ်။ File နဲ့ network handle တွေကို ensure ထဲမှာ ဒါမှမဟုတ် File.open လို block ပုံစံ method တွေနဲ့ အမြဲ လွှတ်ပေးပါတယ်။ Log ရေးတဲ့အခါ class, message, backtrace ကို မှတ်ပြီး cause chain ကို ထိန်းထားပါ၊ exception message ထဲမှာ password, token, ကတ်နံပါတ်တွေ မထည့်ပါနဲ့ ဘာလို့လဲဆိုတော့ message တွေက log နဲ့ error dashboard ထဲ ရောက်လို့ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. rescue Exception ဒါမှမဟုတ် nil ပြန်တဲ့ rescue လွတ်
rescue Exception က exit, Ctrl-C နဲ့ memory ကုန်တာကိုပါ မျိုချလို့ program ကို ရပ်ဖို့ ခက်လာပါတယ်၊ rescue; nil; end လိုမျိုးက အမှားအားလုံးကို တိတ်ဆိတ်စွာ ဖျောက်ပါတယ်။ ကိုင်တွယ်ချင်တဲ့ class ကို နာမည်တပ်ပြီး ရေးပါ၊ မသိတာတွေကို ကျယ်ကျယ်လောင်လောင် ထားပါ။ x rescue nil ဆိုတဲ့ modifier ပုံစံကလည်း StandardError အားလုံးကို ဖျောက်ပါတယ်။
2. ကျယ်တဲ့ clause ကို သီးသန့် clause ရှေ့မှာ ထားတာ
rescue StandardError ကို rescue ArgumentError ရှေ့မှာ ရေးရင် ပထမ clause က ArgumentError ကိုပါ ဖမ်းလို့ ဒုတိယ clause က ဘယ်တော့မှ မ run ပါဘူး၊ Ruby က error မပေးပါဘူး။ သီးသန့်ဆုံးကို အပေါ်ဆုံးမှာ ထားပါ၊ ယေဘုယျ clause ကို အောက်ဆုံးမှာ ထားပါ။
3. ကန့်သတ်ချက်မပါတဲ့ retry
ပျက်နေတဲ့ service အပေါ်မှာ retry ကို counter မပါဘဲ ရေးရင် program က အဆုံးမရှိ loop ဖြစ်ပြီး log ကို ပြည့်စေပါတယ်။ Attempt counter ထားပြီး ကန့်သတ်ချက်ပြည့်ရင် error ကို raise လုပ်ပါ၊ ဖြစ်နိုင်ရင် retry တစ်ကြိမ်ချင်းကြားမှာ ခဏစောင့်ပါ။
4. raise "text" ကို ခေါ်သူက message နဲ့ ခွဲခြားရအောင် လုပ်တာ
raise "invalid" က RuntimeError ပဲ ဖြစ်လို့ ခေါ်သူက message စာသားကို ပိုင်းစစ်မှ ဘာဖြစ်လဲ သိနိုင်ပါတယ်၊ စာသား တစ်လုံးပြောင်းရင် ပျက်ပါတယ်။ ValidationError လို သီးသန့် class တစ်ခု ကြေညာပြီး rescue ValidationError နဲ့ ဖမ်းတာက ခိုင်မာပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- ပျက်နိုင်တဲ့ လိုင်းလေးတွေကိုပဲ begin ထဲ ထည့်ပါ၊ ပြီးရင် ကိုင်တွယ်တတ်တဲ့ class ကိုပဲ rescue လုပ်ပါ။
- File, lock, connection တွေကို ensure ထဲမှာ ဒါမှမဟုတ် block ပုံစံ method တွေနဲ့ လွှတ်ပါ။
- ကိုယ့် app ရဲ့ error တွေအတွက် base class တစ်ခုနဲ့ သီးသန့် subclass တွေ ပြုလုပ်ပါ၊ ရှိပြီးသား error ကို ပြန်ထုပ်ပိုးရင် cause ကို မဆုံးရှုံးစေပါနဲ့။
- Exception message ထဲမှာ လျှို့ဝှက်ချက်တွေ မထည့်ပါနဲ့၊ test မှာလည်း message စာသားထက် class ကို စစ်ပါ။
Mini challenge
Mini challenge: with_retries
TemporaryError ကို အကြိမ်ရေ ကန့်သတ်ပြီး retry လုပ်ပေးပေမယ့် PermanentError ကိုတော့ retry မလုပ်တဲ့ with_retries helper ကို ရေးပါ။
- with_retries(max_attempts) က block ကို attempt နံပါတ်နဲ့ yield လုပ်ရမယ်
- TemporaryError ဆိုရင် "attempt N: message" ကို print ထုတ်ပြီး ကန့်သတ်ချက် မပြည့်သေးရင် retry လုပ်ရမယ်၊ ပြည့်ရင် "gave up after N attempts" ဆိုတဲ့ PermanentError ကို raise လုပ်ရမယ်
- ensure ထဲမှာ "cleanup after attempt N" ကို print ထုတ်ပါ (retry က ensure ကို မ run ဘူးဆိုတာ သတိထားပါ)
- PermanentError ကို မဖမ်းဘဲ ချက်ချင်း အပေါ်ကို ပျံ့ခွင့်ပေးပါ
class TemporaryError < StandardError; end
class PermanentError < StandardError; end
def with_retries(max_attempts)
# TODO: count attempts and yield the attempt number
# TODO: retry on TemporaryError until max_attempts, then raise PermanentError
# TODO: print "cleanup after attempt N" from ensure
end
# TODO: a block that succeeds on the 3rd attempt
# TODO: a block that always fails with TemporaryError, limit 2
# TODO: a block that raises PermanentError at once; it must not be retriedရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: attempt 1: timeout attempt 2: timeout cleanup after attempt 3 result: paid attempt 1: timeout attempt 2: timeout cleanup after attempt 2 failed: gave up after 2 attempts cause: TemporaryError cleanup after attempt 1 failed: card declined
အရိပ်အမြွက် ကြည့်ရန်
begin ရဲ့ အပြင်ဘက်မှာ attempts = 0 ကို ထားပြီး begin ထဲမှာ attempts += 1 လုပ်ပါ၊ ဒါမှ retry လုပ်ရင် counter က မ reset ဖြစ်ပါဘူး။ rescue ထဲမှာ raise PermanentError လုပ်ရင် အဟောင်း TemporaryError က cause အဖြစ် အလိုအလျောက် ပါလာပါလိမ့်မယ်။
အဖြေ ကြည့်ရန်
class TemporaryError < StandardError; end
class PermanentError < StandardError; end
def with_retries(max_attempts)
attempts = 0
begin
attempts += 1
yield attempts
rescue TemporaryError => e
puts "attempt #{attempts}: #{e.message}"
retry if attempts < max_attempts
raise PermanentError, "gave up after #{attempts} attempts"
ensure
puts "cleanup after attempt #{attempts}"
end
end
result = with_retries(3) do |n|
raise TemporaryError, "timeout" if n < 3
"paid"
end
puts "result: #{result}"
begin
with_retries(2) { raise TemporaryError, "timeout" }
rescue PermanentError => e
puts "failed: #{e.message}"
puts "cause: #{e.cause.class}"
end
begin
with_retries(3) { raise PermanentError, "card declined" }
rescue PermanentError => e
puts "failed: #{e.message}"
endQuick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- begin ရဲ့ body က အရင် run ပါတယ်၊ rescue က ကိုက်ညီတဲ့ exception မှာသာ၊ else က exception မရှိမှသာ၊ ensure က အမြဲ run ပါတယ်။
- Rescue clause တွေကို သီးသန့်ကနေ ယေဘုယျ အစီအစဉ်ချပါ၊ rescue Exception ကို ဘယ်တော့မှ မရေးပါနဲ့၊ bare rescue က StandardError ကိုပဲ ဖမ်းပါတယ်။
- raise "text"၊ raise Klass, "msg"၊ raise Klass.new(...)၊ ကွင်းမပါတဲ့ raise နဲ့ cause: ဆိုတဲ့ ပုံစံတွေ ရှိပြီး rescue ထဲက raise အသစ်က cause ကို အလိုအလျောက် မှတ်ပါတယ်။
- ကိုယ့် error တွေကို StandardError subclass အဖြစ် ကြေညာပါ၊ retry ကို အမြဲ ကန့်သတ်ပါ၊ ensure က retry မှာ မ run ဘဲ block ထွက်တဲ့အခါမှ တစ်ကြိမ် run ပါတယ်။
Official Ruby Documentation — Ruby Documentation