Thuta Learning
Java
AdvancedProgrammingအခြေခံ

Exceptions (Try...Catch)

Javaသင်ခန်းစာ 2530 မိနစ်

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

  • try, catch, finally block တွေ ဘယ်အစဉ်နဲ့ run လဲဆိုတာ output ကို ကြိုခန့်မှန်းနိုင်ဖို့
  • Checked exception (throws ကြေညာရတဲ့ဟာ) နဲ့ unchecked RuntimeException ကို ခွဲခြားနိုင်ဖို့
  • Catch block တွေကို specific ကနေ general အစဉ်နဲ့ ရေးပြီး multi-catch သုံးနိုင်ဖို့
  • ကိုယ်ပိုင် exception class ကို ဖန်တီးပြီး မူရင်း cause ကို ပျောက်မသွားအောင် wrap လုပ်နိုင်ဖို့
  • AutoCloseable resource တွေကို try-with-resources နဲ့ အမြဲ ပိတ်နိုင်ဖို့

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

ရိုးရိုးလေး ရှင်းရရင်

Exception ဆိုတာ program run နေရင်း "ဒီအလုပ်ကို ဆက်မလုပ်နိုင်တော့ဘူး" လို့ အော်ပြောလိုက်တဲ့ အချက်ပေးသံတစ်ခုပါ။ Array မှာ မရှိတဲ့ index ကို ယူတာ၊ "two" ကို number ပြောင်းဖို့ ကြိုးစားတာ၊ မရှိတဲ့ file ကို ဖွင့်တာ စတာတွေမှာ Java က exception object တစ်ခု throw လုပ်ပါတယ်။ ဘယ်သူမှ မဖမ်းရင် program တစ်ခုလုံး stack trace နဲ့ ရပ်သွားပါတယ်။

try block ကတော့ "ဒီ code က မှားနိုင်တယ်" လို့ အမှတ်အသားလုပ်တာပါ။ catch block က အချက်ပေးသံကို ဖမ်းပြီး user ကို နားလည်လွယ်တဲ့ message ပြတာ၊ default value သုံးတာလို ပြန်လည်ကောင်းမွန်အောင် လုပ်ပါတယ်။ finally block ကတော့ အောင်မြင်သည်ဖြစ်စေ၊ မှားသည်ဖြစ်စေ အမြဲ run တဲ့ သန့်ရှင်းရေး အပိုင်းပါ။

Problem → Why → Solution

ပြဿနာ: input မှားတာတစ်ခုကြောင့် app တစ်ခုလုံး ပြိုကျတာ

အောက်က program က user ရိုက်ထည့်တဲ့ quantity တွေကို ပေါင်းပါတယ်။ "3" နဲ့ "5" အတွက် အဆင်ပြေပေမယ့် တစ်ယောက်ယောက်က "ten" လို့ ရိုက်လိုက်တာနဲ့ Integer.parseInt() က NumberFormatException throw လုပ်ပြီး main() ကနေ ထွက်သွားပါတယ်။ ကျန်တဲ့ order တွေ မတွက်ရသလို total ကိုလည်း ဘယ်တော့မှ print မထုတ်ရတော့ပါဘူး။

ပိုဆိုးတဲ့ ပုံစံကတော့ ဒီ crash ကို ပျောက်အောင် catch (Exception e) {} လို့ ဗလာ catch ရေးလိုက်တာပါ။ Crash မဖြစ်တော့ပေမယ့် မှားနေတဲ့ data ကို ဘယ်သူမှ မသိတော့ဘဲ total အမှားကြီး ထွက်လာပါတယ်။

Main.java
public class Main {
  public static void main(String[] args) {
    String[] quantities = {"3", "ten", "5"};
    int total = 0;
    for (String q : quantities) {
      total += Integer.parseInt(q); // "ten" -> NumberFormatException, program dies
    }
    System.out.println("Total: " + total); // never reached
  }
}

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

Exception တစ်ခု throw ဖြစ်တဲ့အခါ Java က ကိုက်ညီတဲ့ catch block ကို ရှာဖို့ method call stack ကို အပေါ်ဘက် တစ်ဆင့်ချင်း ပြန်တက်ပါတယ်။ main() အထိ ဘယ်နေရာမှာမှ မတွေ့ရင် thread ရပ်သွားပါတယ်။ ဖြေရှင်းချက်က exception တွေကို ဖုံးကွယ်တာ မဟုတ်ဘဲ ပြန်ကောင်းအောင် လုပ်နိုင်တဲ့ နေရာမှာ specific type နဲ့ ဖမ်းပြီး ဘာမှားလဲဆိုတာ ပြောပြတာပါ။

Core concept: exception တွေ ဘယ်လို ခရီးသွားလဲ

Java မှာ exception တိုင်းက object တစ်ခုပါ၊ class hierarchy ရဲ့ ထိပ်ဆုံးမှာ Throwable ရှိပါတယ်။ Throwable အောက်မှာ အကိုင်းနှစ်ခု ခွဲပါတယ် — OutOfMemoryError လို JVM ကိုယ်တိုင် ပြန်မကောင်းနိုင်တဲ့ Error တွေနဲ့ application code က ကိုင်တွယ်ရမယ့် Exception တွေပါ။ Exception ထဲမှာမှ RuntimeException နဲ့ သူ့ subclass တွေကို unchecked လို့ ခေါ်ပြီး ကျန်တာတွေကို checked လို့ ခေါ်ပါတယ်။

ကွာခြားချက်က compiler မှာ ရှိပါတယ်။ IOException လို checked exception ကို throw လုပ်နိုင်တဲ့ method က ကိုယ်တိုင် catch လုပ်ရမယ်၊ မဟုတ်ရင် signature မှာ throws IOException လို့ ကြေညာရပါမယ်။ မလုပ်ရင် "unreported exception" ဆိုတဲ့ compile error တက်ပါတယ်။ NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException, IllegalArgumentException လို unchecked exception တွေကတော့ များသောအားဖြင့် programming bug ဒါမှမဟုတ် input မှားတာကို ပြလို့ compiler က မဖမ်းခိုင်းပါဘူး။

Exception တစ်ခု throw ဖြစ်တာနဲ့ try block ထဲက ကျန်တဲ့ line တွေကို ကျော်သွားပြီး Java က catch block တွေကို အပေါ်ကနေ အောက်ကို စစ်ပါတယ်။ Exception object ရဲ့ type နဲ့ ပထမဆုံး ကိုက်တဲ့ catch တစ်ခုတည်းပဲ run ပါတယ်။ ဒါကြောင့် specific type တွေကို အပေါ်မှာ၊ general type တွေကို အောက်မှာ ထားရပါတယ် — catch (Exception e) ကို ပထမဆုံး ထားလိုက်ရင် နောက်က catch တွေကို ဘယ်တော့မှ မရောက်နိုင်လို့ compiler က "has already been caught" error ပေးပါတယ်။ Block နှစ်ခုက တူညီတဲ့ အလုပ်လုပ်ရင် catch (NumberFormatException | ArrayIndexOutOfBoundsException e) လို multi-catch နဲ့ ပေါင်းရေးလို့ ရပါတယ်။

finally block ကတော့ try အောင်မြင်ရင်ရော၊ catch run ရင်ရော၊ try ထဲမှာ return လုပ်ရင်တောင် run ပါတယ်။ ဖမ်းမမိတဲ့ exception ဆက်တက်သွားတဲ့အခါမှာလည်း finally က အရင် run ပါတယ်။

ကိုယ်တိုင်လည်း throw new IllegalArgumentException("...") လို throw keyword နဲ့ exception ပစ်လို့ ရပါတယ်။ Method တစ်ခုက သူ့ကတိကို မပြည့်မီနိုင်တဲ့အခါ မှားနေတဲ့ value ကို return ပြန်တာထက် ဘာကြောင့် မရလဲဆိုတဲ့ message ပါတဲ့ exception ပစ်တာက ပိုရှင်းပါတယ်။ Exception ကို ပြန်ကောင်းအောင် လုပ်နိုင်တဲ့ အလွှာမှာပဲ catch လုပ်ပါ၊ မလုပ်နိုင်ရင် ဆက်တက်သွားခွင့်ပေးပါ။

မှတ်ထားရမယ့် ပုံစံ: stack ကို ပြန်တက်တဲ့ အချက်ပေးသံ
  1. try ထဲက line တစ်ခုက throw လုပ်
  2. ကျန်တဲ့ try line တွေကို ကျော်
  3. Type ကိုက်တဲ့ ပထမ catch ကို run
  4. finally ကို အမြဲ run
  5. မကိုက်ရင် caller ဆီ ဆက်တက်

Exception ကို ဖမ်းမယ့်နေရာက ပြန်ကောင်းအောင် လုပ်နိုင်တဲ့ နေရာ ဖြစ်ရပါမယ်။

အထွေထွေ ပုံစံ

java
try {
  riskyWork();
} catch (SpecificException e) {
  recover(e);
} catch (OtherException | ThirdException e) {
  report(e);
} finally {
  cleanUp();
}

void method() throws CheckedException { throw new CheckedException("why"); }

try ထဲမှာ မှားနိုင်တဲ့ code ထားပြီး catch block တွေကို specific ကနေ general အစဉ်နဲ့ ရေးပါတယ်။ | နဲ့ type တွေကို ပေါင်းပြီး multi-catch လုပ်လို့ ရပါတယ်။ finally က optional ဖြစ်ပြီး အမြဲ run ပါတယ်။ Checked exception ကို ကိုယ်တိုင် မဖမ်းရင် method signature မှာ throws နဲ့ ကြေညာရပါတယ်။

Example: input သုံးမျိုးနဲ့ array ကို စစ်ကြည့်ခြင်း

ဒီ example က input သုံးခုကို loop ပတ်ပြီး အောင်မြင်တဲ့ case၊ numbers[5] လို index ကျော်တဲ့ case နဲ့ number မဟုတ်တဲ့ case တွေကို တစ်ခါတည်း ဘေးချင်းယှဉ် မြင်ရအောင် ပြထားပါတယ်။ Catch block တစ်ခုစီက ဘယ် input အတွက် run လဲ၊ finally က ဘယ်အချိန် run လဲဆိုတာ output နဲ့ code ကို တိုက်ကြည့်ပါ။

Main.java
public class Main {
  static int pick(int[] numbers, String rawIndex) {
    int index = Integer.parseInt(rawIndex); // may throw NumberFormatException
    return numbers[index];                  // may throw ArrayIndexOutOfBoundsException
  }

  public static void main(String[] args) {
    int[] numbers = {10, 20, 30};
    String[] inputs = {"1", "5", "two"};

    for (String input : inputs) {
      try {
        System.out.println("Value: " + pick(numbers, input));
      } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Invalid index " + input + ". Choose 0 to 2.");
      } catch (NumberFormatException e) {
        System.out.println("Not a number: " + input);
      } finally {
        System.out.println("Checked input " + input);
      }
    }
  }
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Line 3int index = Integer.parseInt(rawIndex); // may throw NumberFormatException

    "two" လို string ကို int ပြောင်းလို့ မရရင် parseInt() က NumberFormatException ပစ်ပါတယ်။ pick() ထဲမှာ catch မရှိလို့ exception က main() ထဲက try ဆီ တန်းတက်သွားပါတယ်။

  2. Line 4return numbers[index]; // may throw ArrayIndexOutOfBoundsException

    numbers မှာ index 0, 1, 2 ပဲ ရှိလို့ index 5 က ArrayIndexOutOfBoundsException ပစ်ပါတယ်။ Java က array bounds ကို အမြဲ စစ်ပေးလို့ memory ထဲက အမှိုက် value ကို မဖတ်မိပါဘူး။

  3. Line 13System.out.println("Value: " + pick(numbers, input));

    pick() က exception ပစ်ရင် println() ကို လုံးဝ မခေါ်ဘဲ ကျော်သွားပါတယ်။ ဒါကြောင့် "5" နဲ့ "two" အတွက် Value: line မထွက်ပါဘူး။

  4. Lines 14-17} catch (ArrayIndexOutOfBoundsException e) {

    Catch နှစ်ခုက type မတူတဲ့ exception တွေကို သီးသန့် message နဲ့ ကိုင်တွယ်ပါတယ်။ Exception တစ်ခုမှာ ကိုက်တဲ့ catch တစ်ခုတည်းပဲ run ပါတယ်။

  5. Line 18} finally {

    finally က input သုံးခုလုံးအတွက် run ပါတယ် — အောင်မြင်တဲ့ "1" အတွက်ရော၊ catch run ပြီးတဲ့ "5" နဲ့ "two" အတွက်ပါ။ ဒါကြောင့် Checked input line သုံးကြောင်း ထွက်ပါတယ်။

You should see
Value: 20
Checked input 1
Invalid index 5. Choose 0 to 2.
Checked input 5
Not a number: two
Checked input two
try / catch / finally ရဲ့ execution flow
  1. try block ကို စ run
  2. Exception ပစ်လား စစ်
  3. ကိုက်တဲ့ catch ကို ရှာ
  4. finally ကို run

Exception မရှိ

try finishes -> finally -> next line

ကိုက်တဲ့ catch ရှိ

catch -> finally -> next line

ကိုက်တဲ့ catch မရှိ

finally -> exception goes to the caller

ကိုယ်ပိုင် exception နဲ့ throws

Domain rule တစ်ခု ချိုးဖောက်ခံရတဲ့အခါ (ဥပမာ လက်ကျန်ငွေ မလောက်တာ) Exception ကို extend လုပ်ပြီး ကိုယ်ပိုင် checked exception ဖန်တီးလို့ ရပါတယ်။ Checked ဖြစ်လို့ withdraw() ကို ခေါ်တဲ့သူတိုင်းက အဲဒီ case ကို မမေ့ဘဲ ကိုင်တွယ်ရပါတယ်။ Amount က အနုတ်ဖြစ်နေတာလို caller ရဲ့ bug ကိုတော့ unchecked IllegalArgumentException နဲ့ ပြတာ ပိုသင့်တော်ပါတယ်။

Main.java
public class Main {
  static class InsufficientFundsException extends Exception {
    InsufficientFundsException(String message) {
      super(message);
    }
  }

  static int withdraw(int balance, int amount) throws InsufficientFundsException {
    if (amount <= 0) {
      throw new IllegalArgumentException("amount must be positive: " + amount);
    }
    if (amount > balance) {
      throw new InsufficientFundsException("need " + amount + ", have " + balance);
    }
    return balance - amount;
  }

  public static void main(String[] args) {
    try {
      System.out.println("Left: " + withdraw(100, 30));
      System.out.println("Left: " + withdraw(100, 250));
    } catch (InsufficientFundsException e) {
      System.out.println("Declined: " + e.getMessage());
    }
  }
}

ဒီ program က "Left: 70" ပြီးရင် "Declined: need 250, have 100" ကို print ထုတ်ပါတယ်။ Low-level exception တစ်ခုကို ဖမ်းပြီး ပိုအဓိပ္ပာယ်ရှိတဲ့ exception အသစ်နဲ့ ပြန်ပစ်တဲ့အခါ new ConfigException("...", e) လို မူရင်း exception ကို cause အဖြစ် ထည့်ပေးပါ။ Stack trace မှာ "Caused by:" အပိုင်းနဲ့ အရင်းခံ အမှားကို ဆက်မြင်ရပါတယ်။

try-with-resources

File, database connection, network socket လို resource တွေကို သုံးပြီးရင် ပိတ်ရပါတယ်။ try ( ... ) ကွင်းထဲမှာ AutoCloseable object တွေကို ကြေညာရင် Java က block ပြီးတာနဲ့ close() ကို အလိုအလျောက် ခေါ်ပေးပါတယ် — exception ဖြစ်ခဲ့ရင်တောင် ခေါ်ပါတယ်။ Resource တွေကို ဖွင့်ခဲ့တဲ့ အစဉ်ရဲ့ ပြောင်းပြန်နဲ့ ပိတ်ပြီး catch block က resource တွေ ပိတ်ပြီးမှ run ပါတယ်။

Main.java
public class Main {
  static class Connection implements AutoCloseable {
    private final String name;

    Connection(String name) {
      this.name = name;
      System.out.println("open " + name);
    }

    void send(String message) {
      if (message.isEmpty()) {
        throw new IllegalStateException("empty message");
      }
      System.out.println(name + " sent " + message);
    }

    @Override
    public void close() {
      System.out.println("close " + name);
    }
  }

  public static void main(String[] args) {
    try (Connection db = new Connection("db");
         Connection cache = new Connection("cache")) {
      db.send("hello");
      cache.send("");
    } catch (IllegalStateException e) {
      System.out.println("caught: " + e.getMessage());
    }
  }
}

Output က open db, open cache, db sent hello, close cache, close db, caught: empty message အစဉ်အတိုင်း ထွက်ပါတယ်။ cache ကို နောက်မှ ဖွင့်လို့ အရင် ပိတ်ပြီး catch က အဆုံးမှ run တာကို သတိထားကြည့်ပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Backend service တစ်ခုမှာ exception တွေကို အလွှာလိုက် ကိုင်တွယ်ပါတယ်။ Repository အလွှာက database driver ဆီက SQLException ကို ဖမ်းပြီး OrderNotFoundException လို domain exception အဖြစ် cause နဲ့တကွ ပြန်ပစ်ပါတယ်။ Service အလွှာက domain rule တွေကို စစ်ပြီး controller အလွှာ (ဥပမာ Spring ရဲ့ global exception handler) က exception type တစ်ခုစီကို 404, 400, 500 လို HTTP status နဲ့ client နားလည်တဲ့ JSON message အဖြစ် ပြောင်းပါတယ်။

Log တွေမှာ stack trace အပြည့်နဲ့ cause chain ကို ရေးထားလို့ production မှာ ပြဿနာတက်တဲ့အခါ အရင်းခံကို ရှာနိုင်ပါတယ်၊ user ကိုတော့ internal detail မပါတဲ့ message ပဲ ပြပါတယ်။ File ဖတ်တာ၊ HTTP client ခေါ်တာ၊ database connection ယူတာတွေမှာ try-with-resources ကို စံအဖြစ် သုံးပါတယ်၊ resource leak က ချက်ချင်း မပေါ်ဘဲ server က နာရီပေါင်းများစွာ run ပြီးမှ connection ကုန်သွားတဲ့ ပုံစံနဲ့ ပေါ်လာတတ်လို့ပါ။ Test တွေမှာလည်း assertThrows နဲ့ မှားတဲ့ input ပေးရင် မှန်ကန်တဲ့ exception type ထွက်လားဆိုတာ စစ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Exception ကို ဗလာ catch နဲ့ မျိုချတာ

catch (Exception e) {} က crash ကို ဖျောက်ပေမယ့် bug ကိုပါ ဖျောက်ပစ်ပါတယ်။ Program က မှားနေတဲ့ state နဲ့ ဆက် run ပြီး ပြဿနာက နောက်မှ နားမလည်နိုင်တဲ့ နေရာမှာ ပေါ်လာပါတယ်။ ပြန်ကောင်းအောင် မလုပ်နိုင်ရင် မဖမ်းပါနဲ့၊ ဖမ်းရင် အနည်းဆုံး log ရေးပြီး ဘာဆက်လုပ်မလဲ ဆုံးဖြတ်ပါ။

2. Wrap လုပ်တဲ့အခါ cause ကို ပစ်ပစ်တာ

throw new RuntimeException("load failed"); လို့ပဲ ရေးရင် မူရင်း IOException ရဲ့ message နဲ့ stack trace ပျောက်သွားပါတယ်။ throw new RuntimeException("load failed", e); လို့ e ကို ဒုတိယ argument အဖြစ် ထည့်ပါ၊ ဒါမှ "Caused by:" မှာ file နာမည်နဲ့ line number ကို ဆက်မြင်ရပါမယ်။

3. General catch ကို အပေါ်ဆုံးမှာ ထားတာ

catch (Exception e) ကို catch (NumberFormatException e) ရဲ့ အပေါ်မှာ ထားရင် ဒုတိယ block ကို ဘယ်တော့မှ မရောက်နိုင်လို့ compile error တက်ပါတယ်။ Subclass ကို အမြဲ superclass ရဲ့ အပေါ်မှာ ရေးပါ။

4. finally ထဲမှာ return လုပ်တာ

finally ထဲက return က try ထဲမှာ ပစ်ထားတဲ့ exception ကို အသံမထွက်ဘဲ ဖျက်ပစ်ပါတယ်။ finally ကို သန့်ရှင်းရေးအတွက်ပဲ သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • ကိုင်တွယ်နိုင်တဲ့ အတိအကျ type ကိုပဲ ဖမ်းပါ၊ catch (Exception e) ကို program ရဲ့ အပြင်ဆုံး boundary မှာပဲ သုံးပါ။
  • Exception message မှာ မှားခဲ့တဲ့ value ကို ထည့်ပါ — "port out of range: 70000" က "invalid input" ထက် အများကြီး အသုံးဝင်ပါတယ်။
  • Close လုပ်ရမယ့် object မှန်သမျှကို try-with-resources နဲ့ ဖွင့်ပါ။
  • ပုံမှန် control flow (loop ထွက်တာ၊ value ရှိမရှိ စစ်တာ) အတွက် exception မသုံးပါနဲ့၊ if နဲ့ စစ်လို့ရရင် if ကို သုံးပါ။

Mini challenge

Mini challenge: config port ကို စစ်ဆေးခြင်း

Config file ကနေ ဖတ်လာတဲ့ port string တွေကို စစ်တဲ့ parsePort() method ရေးပါ။ မှားတဲ့ case တိုင်းမှာ ကိုယ်ပိုင် checked exception ConfigException ကို ပစ်ပြီး number ပြောင်းလို့ မရတဲ့ case မှာ မူရင်း NumberFormatException ကို cause အဖြစ် ထိန်းထားရပါမယ်။

  • ConfigException က Exception ကို extend လုပ်ပြီး (message) နဲ့ (message, cause) constructor နှစ်ခု ရှိရမယ်
  • null ဒါမှမဟုတ် blank string ဆိုရင် "port is missing"
  • Number မဟုတ်ရင် NumberFormatException ကို cause အဖြစ် wrap လုပ်ရမယ်
  • 1 ကနေ 65535 အပြင်ဘက် ဆိုရင် "port out of range: <port>"
  • main() က input တစ်ခုစီရဲ့ ရလဒ် ဒါမှမဟုတ် message နဲ့ cause class နာမည်ကို print ထုတ်ရမယ်
Main.java
public class Main {
  static class ConfigException extends Exception {
    // TODO: constructor (String message)
    // TODO: constructor (String message, Throwable cause)
  }

  static int parsePort(String raw) throws ConfigException {
    // TODO: reject null or blank input
    // TODO: parse with Integer.parseInt, wrapping NumberFormatException as the cause
    // TODO: reject ports outside 1..65535
    return 0;
  }

  public static void main(String[] args) {
    String[] inputs = {"8080", " 443 ", "http", "70000", ""};
    // TODO: for each input print "OK <port>" or "Rejected: <message> (cause: <name>)"
  }
}

ရလဒ်: Program က ဒီ line ငါးကြောင်းကို print ထုတ်ရပါမယ်: OK 8080 OK 443 Rejected: port is not a number: http (cause: NumberFormatException) Rejected: port out of range: 70000 (cause: none) Rejected: port is missing (cause: none)

အရိပ်အမြွက် ကြည့်ရန်

Constructor ထဲမှာ super(message, cause) ကို ခေါ်ရင် Exception က cause ကို သိမ်းပေးပြီး e.getCause() နဲ့ ပြန်ယူလို့ ရပါတယ်။ Cause class နာမည်အတွက် getClass().getSimpleName() ကို သုံးပါ၊ cause မရှိရင် getCause() က null ပြန်ပါတယ်။

အဖြေ ကြည့်ရန်
Main.java
public class Main {
  static class ConfigException extends Exception {
    ConfigException(String message) {
      super(message);
    }

    ConfigException(String message, Throwable cause) {
      super(message, cause);
    }
  }

  static int parsePort(String raw) throws ConfigException {
    if (raw == null || raw.isBlank()) {
      throw new ConfigException("port is missing");
    }
    int port;
    try {
      port = Integer.parseInt(raw.trim());
    } catch (NumberFormatException e) {
      throw new ConfigException("port is not a number: " + raw, e);
    }
    if (port < 1 || port > 65535) {
      throw new ConfigException("port out of range: " + port);
    }
    return port;
  }

  public static void main(String[] args) {
    String[] inputs = {"8080", " 443 ", "http", "70000", ""};
    for (String input : inputs) {
      try {
        System.out.println("OK " + parsePort(input));
      } catch (ConfigException e) {
        Throwable cause = e.getCause();
        String causeName = cause == null ? "none" : cause.getClass().getSimpleName();
        System.out.println("Rejected: " + e.getMessage() + " (cause: " + causeName + ")");
      }
    }
  }
}

Quick Quiz 1 / 3

try { print("A"); int x = 10 / 0; print("B"); } catch (ArithmeticException e) { print("C"); } finally { print("D"); } က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

ဘယ် exception ကို ပစ်နိုင်တဲ့ method က throws ကြေညာဖို့ ဒါမှမဟုတ် catch လုပ်ဖို့ compiler က မဖြစ်မနေ တောင်းဆိုလဲ။

Quick Quiz 3 / 3

catch (IOException e) { throw new IllegalStateException("cannot load settings"); } ရဲ့ ပြဿနာက ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Exception တစ်ခု throw ဖြစ်ရင် try ရဲ့ ကျန်တဲ့ line တွေကို ကျော်ပြီး type ကိုက်တဲ့ ပထမ catch ကို run ပါတယ်၊ မကိုက်ရင် caller ဆီ ဆက်တက်ပါတယ်။
  • Checked exception (ဥပမာ IOException) ကို catch ဒါမှမဟုတ် throws လုပ်ရပြီး RuntimeException အုပ်စုကတော့ unchecked ပါ။
  • Catch တွေကို specific ကနေ general အစဉ်နဲ့ ရေးပါ၊ finally က အမြဲ run ပါတယ်။
  • Wrap လုပ်တဲ့အခါ cause ကို ထည့်ပါ၊ resource တွေကို try-with-resources နဲ့ ပိတ်ပါ။

Java SE 25 API Specification — Oracle

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

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

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

Exceptions (Try...Catch) | Thuta Learning