Python သည် Keyword မှလွဲ၍
ဥပမာ
ထုတ်ပြန်ချက်တွင် အမှားအယွင်းတစ်ခု ပေါ်ပေါက်လာပါက "တစ်စုံတစ်ခု မှားယွင်းနေသည်" ကို ပရင့်ထုတ်ခြင်း-
try:
x > 3
except:
print("Something went wrong")
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
except
ပိတ်ဆို့ခြင်းမှလွဲ၍ ကြိုးစားရာတွင် အဓိကစကားလုံးကို အသုံးပြုသည် ။ try block သည် error တက်လာပါက run ရန် code ၏ block တစ်ခုကို သတ်မှတ်သည်။
မတူညီသော error အမျိုးအစားများအတွက် မတူညီသော block များကို သင်သတ်မှတ်နိုင်ပြီး ဘာမှမမှားပါက လုပ်ဆောင်ရန် blocks များကို အောက်တွင် နမူနာကြည့်ပါ။
နောက်ထပ် ဥပမာများ
ဥပမာ
NameError ဖြစ်ပါက မက်ဆေ့ချ်တစ်ခုရေးပါ၊ နောက်တစ်ခုက TypeError ဖြစ်ပါက၊
x = "hello"
try:
x > 3
except NameError:
print("You have a variable that is not defined.")
except TypeError:
print("You are comparing values of different type")
ဥပမာ
error ပေါ်ပေါက်စေသော ထုတ်ပြန်ချက်တစ်ခုကို လုပ်ဆောင်ရန် ကြိုးစားသော်လည်း သတ်မှတ်ထားသော အမှားအမျိုးအစားများ (ဤကိစ္စတွင်၊ ZeroDivisionError)
try:
x = 1/0
except NameError:
print("You have a
variable that is not defined.")
except TypeError:
print("You
are comparing values of different type")
except:
print("Something else went wrong")
ဥပမာ
အမှားအယွင်းများ မရှိပါက မက်ဆေ့ချ် ရေးပါ။
x = 1
try:
x > 10
except NameError:
print("You have a variable that is not defined.")
except TypeError:
print("You are comparing values of different type")
else:
print("The 'Try' code was executed without raising any errors!")
ဆက်စပ်စာမျက်နှာများ
အဓိက try
စကားလုံး။
အဓိက finally
စကားလုံး။