Thuta Learning
C#
AdvancedProgrammingအခြေခံ

Exceptions

C#သင်ခန်းစာ 2325 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Exceptions ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Normal, boundary နဲ့invalid cases ကိုကိုယ်တိုင်စမ်းနိုင်ရန်
  • Readable, type-safe နဲ့testable C# code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

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 ကိုထိန်းပါ။

Mental Model
  1. Input / fallible work
  2. Validate + execute + dispose
  3. Value / exception

Exceptions ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

csharp
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 ကို အဆင့်လိုက်ဖတ်မယ်

  1. Input / fallible worktry

    Program တစ်ခုမှာ error ဖြစ်နိုင်တဲ့နေရာတွေ အမြဲရှိပါတယ်။ File မတွေ့တာ၊ network fail ဖြစ်တာ၊ array index မှားတာ၊ user input မမှန်တာ စတာတွေပါ။ Exception Handling က error ဖြစ်တဲ့အခါ app တစ်ခုလုံး crash မဖြစ်အောင် လုံခြုံစွာကိုင်တွယ်ပေးပါတယ်။

  2. Validate + execute + disposecatch (Exception ex)

    Exceptions သည်unexpected failure flow အတွက်ဖြစ်ပြီး expected validation branch အားလုံးအတွက်မဟုတ်ပါ။ Specific exceptions ကိုcatch လုပ်၊ context ပါအောင်log/rethrow လုပ်ပြီး resource cleanup အတွက်`using`/`finally` သုံးကာ stack trace ကိုထိန်းပါ။

  3. Value / exception}

    File read sample တွင်success, missing file, invalid content နဲ့cleanup cases စမ်းပြီး user message နဲ့diagnostic detail ကိုခွဲပါ။

Execution Flow
  1. Input / fallible work
  2. Validate + execute + dispose
  3. 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 မဖြစ်အောင် လုံခြုံစွာကိုင်တွယ်ပေးပါတယ်။

csharp
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 လုပ်ဖို့အသုံးဝင်ပါတယ်။
You should see
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 ကိုခန့်မှန်းပါ
csharp
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

Exceptions ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • 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

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Empty `catch` သို့`throw ex` သုံးခြင်းကfailure ကိုဖုံးပြီးoriginal stack trace ဆုံးရှုံးစေပါတယ်။
  • Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

File read sample တွင်success, missing file, invalid content နဲ့cleanup cases စမ်းပြီး user message နဲ့diagnostic detail ကိုခွဲပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Index is outside the array range. Finished checking the array.

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Exceptions | Thuta Learning