ရိုးရိုးလေး ရှင်းပြမယ်
Exceptions ကို ရိုးရိုးလေးနားလည်မယ်
Program တစ်ခုမှာ error ဖြစ်နိုင်တဲ့နေရာတွေ အမြဲရှိပါတယ်။ File မတွေ့တာ၊ network fail ဖြစ်တာ၊ array index မှားတာ၊ user input မမှန်တာ စတာတွေပါ။ Exception Handling က error ဖြစ်တဲ့အခါ app တစ်ခုလုံး crash မဖြစ်အောင် လုံခြုံစွာကိုင်တွယ်ပေးပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Empty `catch` သို့`throw ex` သုံးခြင်းကfailure ကိုဖုံးပြီးoriginal stack trace ဆုံးရှုံးစေပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Exceptions သည်unexpected failure flow အတွက်ဖြစ်ပြီး expected validation branch အားလုံးအတွက်မဟုတ်ပါ။ Specific exceptions ကိုcatch လုပ်၊ context ပါအောင်log/rethrow လုပ်ပြီး resource cleanup အတွက်`using`/`finally` သုံးကာ stack trace ကိုထိန်းပါ။
- Input / fallible work
- Validate + execute + dispose
- Value / exception
Exceptions ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
try
{
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[10]);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Index is outside the array range.");
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong.");
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finished checking the array.");
}Exceptions သည်unexpected failure flow အတွက်ဖြစ်ပြီး expected validation branch အားလုံးအတွက်မဟုတ်ပါ။ Specific exceptions ကိုcatch လုပ်၊ context ပါအောင်log/rethrow လုပ်ပြီး resource cleanup အတွက်`using`/`finally` သုံးကာ stack trace ကိုထိန်းပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input / fallible work
tryProgram တစ်ခုမှာ error ဖြစ်နိုင်တဲ့နေရာတွေ အမြဲရှိပါတယ်။ File မတွေ့တာ၊ network fail ဖြစ်တာ၊ array index မှားတာ၊ user input မမှန်တာ စတာတွေပါ။ Exception Handling က error ဖြစ်တဲ့အခါ app တစ်ခုလုံး crash မဖြစ်အောင် လုံခြုံစွာကိုင်တွယ်ပေးပါတယ်။
- Validate + execute + dispose
catch (Exception ex)Exceptions သည်unexpected failure flow အတွက်ဖြစ်ပြီး expected validation branch အားလုံးအတွက်မဟုတ်ပါ။ Specific exceptions ကိုcatch လုပ်၊ context ပါအောင်log/rethrow လုပ်ပြီး resource cleanup အတွက်`using`/`finally` သုံးကာ stack trace ကိုထိန်းပါ။
- Value / exception
}File read sample တွင်success, missing file, invalid content နဲ့cleanup cases စမ်းပြီး user message နဲ့diagnostic detail ကိုခွဲပါ။
- Input / fallible work
- Validate + execute + dispose
- Value / exception
Success
Exceptions ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Empty `catch` သို့`throw ex` သုံးခြင်းကfailure ကိုဖုံးပြီးoriginal stack trace ဆုံးရှုံးစေပါတယ်။
Program တစ်ခုမှာ error ဖြစ်နိုင်တဲ့နေရာတွေ အမြဲရှိပါတယ်။ File မတွေ့တာ၊ network fail ဖြစ်တာ၊ array index မှားတာ၊ user input မမှန်တာ စတာတွေပါ။ Exception Handling က error ဖြစ်တဲ့အခါ app တစ်ခုလုံး crash မဖြစ်အောင် လုံခြုံစွာကိုင်တွယ်ပေးပါတယ်။
try
{
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[10]);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Index is outside the array range.");
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong.");
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finished checking the array.");
}try, catch, finally
tryထဲမှာ error ဖြစ်နိုင်တဲ့ code ကိုထားပါတယ်။catchက error ဖြစ်လာရင် ကိုင်တွယ်ပါတယ်။ Specific exception ကိုအရင်ဖမ်းတာက ပိုကောင်းပါတယ်။finallyက error ဖြစ်ဖြစ် မဖြစ်ဖြစ် run ပါတယ်။ Resource cleanup လုပ်ဖို့အသုံးဝင်ပါတယ်။
Index is outside the array range. Finished checking the array.Info
⚠️ Best practice
Error ကို မျက်လုံးပိတ်ပြီးဖျောက်ပစ်တာမျိုး မလုပ်ပါနဲ့။ User ကို friendly message ပြပြီး developer အတွက် log သိမ်းထားတာက ပိုကောင်းပါတယ်။
Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
File read sample တွင်success, missing file, invalid content နဲ့cleanup cases စမ်းပြီး user message နဲ့diagnostic detail ကိုခွဲပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
File read sample တွင်success, missing file, invalid content နဲ့cleanup cases စမ်းပြီး user message နဲ့diagnostic detail ကိုခွဲပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
try
{
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[10]);
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("Index is outside the array range.");
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong.");
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Finished checking the array.");
}ရလဒ်: Exceptions ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
File read sample တွင်success, missing file, invalid content နဲ့cleanup cases စမ်းပြီး user message နဲ့diagnostic detail ကိုခွဲပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input / fallible work → Validate + execute + dispose → Value / exception
- Exceptions သည်unexpected failure flow အတွက်ဖြစ်ပြီး expected validation branch အားလုံးအတွက်မဟုတ်ပါ။ Specific exceptions ကိုcatch လုပ်၊ context ပါအောင်log/rethrow လုပ်ပြီး resource cleanup အတွက်`using`/`finally` သုံးကာ stack trace ကိုထိန်းပါ။
- Empty `catch` သို့`throw ex` သုံးခြင်းကfailure ကိုဖုံးပြီးoriginal stack trace ဆုံးရှုံးစေပါတယ်။
- File read sample တွင်success, missing file, invalid content နဲ့cleanup cases စမ်းပြီး user message နဲ့diagnostic detail ကိုခွဲပါ။
Exceptions and exception handling — Microsoft Learn