Python ကို နောက်ဆုံးအနေနဲ့ စမ်းကြည့်ပါ။
နောက်ဆုံးကြိုးစားပါ။
သတ်မှတ်ထား finally
ပါက၊ ကြိုးစားပိတ်ဆို့ခြင်းသည် အမှားအယွင်းတစ်ခု ပေါ်ပေါက်လာပါက မည်သို့ပင်ဆိုစေကာမူ ပိတ်ဆို့ခြင်းကို လုပ်ဆောင်ပါမည်။
ဥပမာ
try:
print(x)
except:
print("Something went
wrong")
finally:
print("The 'try except' is finished")
အရာဝတ္ထုများကို ပိတ်ပြီး အရင်းအမြစ်များကို ရှင်းလင်းရန် ၎င်းသည် အသုံးဝင်နိုင်သည်-
ဥပမာ
ဖွင့်၍မရသော ဖိုင်တစ်ခုသို့ စာရေးရန် ကြိုးစားပါ-
try:
f = open("demofile.txt")
try:
f.write("Lorum Ipsum")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
except:
print("Something went wrong when opening the
file")
ဖိုင်အရာဝတ္တုကို ဖွင့်မထားဘဲ ပရိုဂရမ်သည် ဆက်လက်လုပ်ဆောင်နိုင်သည်။