การ Throw

ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

Jim White

Java Developer

ตัวเลือกในการจัดการ

  • Checked exception ต้องมีการ "จัดการ"
    • การ "จัดการ" runtime exception เป็นทางเลือก
  • มี 2 วิธีในการ "จัดการ"
    • ใช้ try/catch เพื่อดักจับ Exception และเขียนโค้ดแก้ไขปัญหา
    • "Throw" exception นั้นออกไป
  • Try/catch เป็นวิธีที่แนะนำในการจัดการ exception
    • แต่ไม่สามารถทำได้เสมอไป และบางครั้งการรวมการจัดการ exception ไว้ที่เดียวก็ดีกว่า
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

คีย์เวิร์ด throws

  • ใช้ throws กับเมธอดใดก็ได้เพื่อส่ง exception กลับไปยังเมธอดที่เรียก
    • แทนที่จะจัดการ exception ในเมธอดนั้นเอง
    • เรียกว่า "การ throw exception" หรือ "การโยนความรับผิดชอบ"
    • ส่งความรับผิดชอบในการจัดการ exception ให้กับผู้เรียก
  • ใช้รายการประเภท exception คั่นด้วยจุลภาคหลัง throws
    • ระบุประเภท exception ที่เมธอดผู้เรียกอาจต้องเตรียมรับมือ
public static void someMethod() throws IndexOutOfBoundsException, 
NegativeArraySizeException, NullPointerException {
  // method code
}
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java
ตัวอย่าง throws

ใช้ try-catch ปกติในการจัดการ Exception

public static void main(String[] args) {
  someMethod();
}
public static void someMethod(){
  try {
    ArrayList<String> games
      = new ArrayList<String>();
    games.add("Monopoly");
    games.add("Chess");
    games.get(3);
  } catch (IndexOutOfBoundsException e) {
    System.out.println(
   "Oops - trying to get non-existent item");
  }
}

ใช้ throws ในการจัดการ Exception

public static void main(String[] args) {
  try {
    someMethod();
  } catch (IndexOutOfBoundsException e) {
    System.out.println(
   "someMethod tried to get non-existent item");
  }
}
public static void someMethod()
      throws IndexOutOfBoundsException {
  ArrayList<String> games
    = new ArrayList<String>();
  games.add("Monopoly");
  games.add("Chess");
  games.get(3);
}
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

เมื่อใดควรใช้ throws

  • การ "โยนความรับผิดชอบ" หรือการ throw exception เป็นสิ่งที่ควรหลีกเลี่ยง
    • โดยปกติแนะนำให้ใช้ try-catch ตรงจุดที่เกิด exception
  • เหตุผลที่เลือก throw แทน try-catch ได้แก่:
    • เมธอดที่เกิด exception อาจไม่รู้วิธีกู้คืน
    • รวมการจัดการไว้ที่เดียวเพื่อลดโค้ด try/catch ที่ซ้ำซ้อน

การ throw exception มักใช้ร่วมกับ custom exception

1 Photo from https://unsplash.com/@philipparosetite
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

การดักจับและ rethrow

  • Exception สามารถ "rethrow" ได้
    • เมธอดผู้เรียกก็ใช้ throws แทนการมี try/catch
  • การ rethrow จาก main จะทำให้แอปพลิเคชันหยุดทำงาน

Exception สามารถ rethrow ไปยังเมธอดที่เรียกได้

ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

ตัวอย่างการ rethrow

public class RethrowExample {
    public static void main(String[] args) {
        try {
            method1(0);
        } catch (ArithmeticException e) {
            System.out.println("Oops - tried a bad quotient");
        }
    }
    public static void method1(int divisor) throws ArithmeticException {
        method2(divisor);
    }
    public static void method2 (int divisor) throws ArithmeticException {
        int z = 5/divisor;
        System.out.println(z);
    }
}
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

มาฝึกกันเถอะ!

ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

Preparing Video For Download...