Debugging — Error တွေကို ဖတ်ပြီး ဖြေရှင်းနည်း
Debugging ဆိုတာ Code ထဲမှာ ဘာမှားနေသလဲ ရှာပြီး ပြင်တဲ့ လုပ်ငန်းစဉ်ပါ။ Programmer တိုင်းဟာ Error တွေ တွေ့ပါတယ် — ကွာတာကတော့ Error ကို ဘယ်လောက်မြန်မြန် ဖြေရှင်းနိုင်တာပါ။
Python Error Traceback ကို ဖတ်နည်း
Section titled “Python Error Traceback ကို ဖတ်နည်း”Error တက်တဲ့အခါ Python က Traceback ဆိုတဲ့ Error Report ကို ပေးပါတယ်။ ဒါကို ကောင်းကောင်း ဖတ်တတ်ဖို့ အရေးကြီးပါတယ်:
def calculate(a, b): return a / b
result = calculate(10, 0)print(result)Traceback (most recent call last): File "main.py", line 4, in <module> result = calculate(10, 0) File "main.py", line 2, in calculate return a / bZeroDivisionError: division by zeroဖတ်နည်း:
- အောက်ဆုံး Line ကို ဦးစွာ ကြည့်ပါ →
ZeroDivisionError: division by zero← ဒါက Error အမျိုးအစားနဲ့ အကြောင်းပါ - File name နှင့် Line number ကို ကြည့်ပါ →
File "main.py", line 2← ဒါက Error တက်တဲ့ နေရာပါ - Error ဖြစ်ပြီးသား Code ကို ကြည့်ပါ →
return a / b← ဒါက ပြဿနာ ဖြစ်တဲ့ Code ပါ
အဖြစ်များသော Error မျိုး ၆ ခု
Section titled “အဖြစ်များသော Error မျိုး ၆ ခု”၁. SyntaxError — Code ရေးပုံ မှားနေသည်
Section titled “၁. SyntaxError — Code ရေးပုံ မှားနေသည်”# Errorprint("မင်္ဂလာ" # Closing ) မပါ
# SyntaxError: '(' was never closedဖြေရှင်းနည်း: Error Line ကိုသွားပြီး Bracket, Quote, Colon (:), Indentation တွေကို စစ်ဆေးပါ။
၂. NameError — Variable / Function မသိ
Section titled “၂. NameError — Variable / Function မသိ”print(massage) # message ကို massage လို့ မှားရိုက်မိ# NameError: name 'massage' is not definedဖြေရှင်းနည်း: Variable နာမည် မှန်မမှန် စစ်ဆေးပါ။ Python က Case-sensitive ဖြစ်တာကြောင့် Name နဲ့ name မတူပါဘူး။
၃. TypeError — Data Type မကိုက်ညီ
Section titled “၃. TypeError — Data Type မကိုက်ညီ”age = "20"print(age + 1)# TypeError: can only concatenate str (not "int") to strဖြေရှင်းနည်း: Type ကို စစ်ဆေးပြီး int(age) / str(age) နဲ့ ပြောင်းပါ။
၄. IndexError — Index ကျော်နေသည်
Section titled “၄. IndexError — Index ကျော်နေသည်”fruits = ["Apple", "Banana"]print(fruits[5]) # Index 5 မရှိဘူး# IndexError: list index out of rangeဖြေရှင်းနည်း: len(fruits) ကိုစစ်ဆေးပြီး Index 0 ကနေ len-1 အထိပဲ ရှိကြောင်း မှတ်ပါ။
၅. KeyError — Dictionary Key မရှိ
Section titled “၅. KeyError — Dictionary Key မရှိ”student = {"name": "Aung", "age": 20}print(student["phone"])# KeyError: 'phone'ဖြေရှင်းနည်း: .get("phone", "မရှိ") ကိုသုံးပြီး Default Value ထည့်ပါ။
၆. ValueError — Value ပုံစံ မှားနေသည်
Section titled “၆. ValueError — Value ပုံစံ မှားနေသည်”age = int("hello")# ValueError: invalid literal for int() with base 10: 'hello'ဖြေရှင်းနည်း: User Input ကို try/except ValueError နဲ့ ထိန်းချုပ်ပါ။
Print Debugging — အမြန်ဆုံး Debugging နည်း
Section titled “Print Debugging — အမြန်ဆုံး Debugging နည်း”Error မရှင်းရင် Code ရဲ့ သင်ယူလိုသောနေရာမှာ print() ထည့်ပြီး Variable တန်ဖိုးတွေကို စစ်ဆေးတာဟာ Beginner Programmer တွေ အကောင်းဆုံး သုံးနိုင်သော နည်းပါ:
def calculate_total(prices): print(f"DEBUG: prices = {prices}") # ← ဒီမှာ ဘာ ရောက်လာသလဲ?
total = 0 for price in prices: print(f"DEBUG: adding {price}, total so far = {total}") total += price
print(f"DEBUG: final total = {total}") return total
items = [100, 250, 75]result = calculate_total(items)print(f"Final: {result}")DEBUG: prices = [100, 250, 75]DEBUG: adding 100, total so far = 0DEBUG: adding 250, total so far = 100DEBUG: adding 75, total so far = 350DEBUG: final total = 425Final: 425Code ပြင်ပြီးရင် DEBUG print တွေကို ဖြုတ်ပစ်ပါ (သို့) # comment ဖြင့် ပိတ်ပါ။
assert — ကိုယ်ကိုယ်ကို စစ်ဆေးခြင်း
Section titled “assert — ကိုယ်ကိုယ်ကို စစ်ဆေးခြင်း”assert ဟာ “ဒါ မှန်ရမည်” ဆိုပြီး Code ထဲမှာ Check Point ထည့်တဲ့ နည်းပါ:
def divide(a, b): assert b != 0, "b ကို 0 ဖြင့် မထားနိုင်ပါ!" return a / b
print(divide(10, 2)) # 5.0print(divide(10, 0)) # AssertionError: b ကို 0 ဖြင့် မထားနိုင်ပါ!VS Code Debugger — Breakpoint သုံးနည်း
Section titled “VS Code Debugger — Breakpoint သုံးနည်း”VS Code မှာ Breakpoint ထည့်ပြီး Code ကို တစ်ကြောင်းချင်းစီ Run ကြည့်နိုင်ပါတယ်:
- VS Code မှာ Code Line ဘယ်ဘက် Number ကို နှိပ်ပြီး 🔴 (Red Dot) ထည့်ပါ — ဒါ Breakpoint ပါ
- F5 သို့မဟုတ် Run → Start Debugging ကို နှိပ်ပါ
- Code ဟာ Breakpoint မှာ ရပ်မှာပါ
- F10 = တစ်ကြောင်းဆင်းရန် (Step Over)
- F11 = Function ထဲဝင်ကြည့်ရန် (Step Into)
- ဘယ်ဘက်မှာ Variables Panel ကို ကြည့်ပြီး တန်ဖိုးများ စစ်ဆေးနိုင်သည်
Debugging Mindset
Section titled “Debugging Mindset”Error တွေ တွေ့ရင် ဒီ Steps တွေ လိုက်နာပါ:
- Error Message ကို ဖတ်ပါ — ပထမ ၂-၃ ကြောင်းကိုပဲ ဖတ်ကြည့်ပါ၊ ဘာ Error လဲ နားလည်ပါ
- Line Number ကို ကြည့်ပါ — Error ဖြစ်တဲ့ Line ကို သွားပါ
- Assumptions စစ်ဆေးပါ — Variable တန်ဖိုးတွေ ထင်သလောက် ဟုတ်မဟုတ် Print ကြည့်ပါ
- တစ်ခုချင်းစီ ပြင်ပါ — တစ်ကြိမ်မှာ တစ်ခုပဲ ပြင်ပြီး Run ကြည့်ပါ
- မဖြေရှင်းနိုင်ရင် — Error Message ကို Google မှာ ရှာပါ (Stack Overflow မှာ အဖြေ ရှိတတ်ပါတယ်)