Try and Catch

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

Jim White

Java Developer

ปัญหาใน Java

  • ปัญหาหรือข้อผิดพลาดสามารถเกิดขึ้นได้ในแอปพลิเคชัน
    • เนื่องจากข้อมูลนำเข้าไม่ถูกต้อง
    • เนื่องจากข้อผิดพลาดในโค้ด
    • เนื่องจากสถานการณ์ที่คาดไม่ถึง
  • ปัญหาหรือข้อผิดพลาดเหล่านี้เรียกว่า exceptions
    • ขัดจังหวะการทำงานปกติของโปรแกรม
  • Java throws exceptions
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

ตัวอย่าง Exception

public static void main(String[] args) {
    ArrayList<String> allStars = new ArrayList<String>();
    allStars.add("Mays");
    allStars.add("Aaron");
    allStars.add("Ruth");
    String last = allStars.get(3); // This will cause an exception
    System.out.println(last);
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of 
bounds for length 3
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

Try

  • ครอบโค้ดที่อาจเกิดข้อผิดพลาดด้วย try-block
    • "ลองรันโค้ดนี้ หากมีปัญหาให้ส่งต่อไปยัง catch block"
try {
    // Statements to be executed and watched for exceptions
}
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

Catch

  • วาง catch-block ต่อจาก try
    • ทำหน้าที่เป็น handler สำหรับจัดการข้อผิดพลาด
    • exception ที่ถูกดักจับจะถูกส่งเข้า block นี้เป็น parameter
  • try/catch blocks จำเป็นต้องใช้กับ exception บางประเภท
    • บางประเภทใช้หรือไม่ก็ได้
try {
    // Statements to be executed and watched for exceptions
} catch (<Exception type> <variable name>) {
    // Statements to execute if an exception has occurred
}
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

การดักจับ IndexOutOfBoundsException

try {
  ArrayList<String> allStars = new ArrayList<String>();
  allStars.add("Mays");
  allStars.add("Aaron");
  allStars.add("Ruth");
  String last = allStars.get(3); // Accessing an element that isn't there
  System.out.println(last);
} catch (IndexOutOfBoundsException e) {
  System.out.println("Oops - wrong index");
}
Oops - wrong index
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

Multiple Catch

  • ใช้ catch หลายบล็อกเพื่อจัดการ exception ต่างประเภทกัน
  • แต่ละ catch มี exception type และตัวแปร exception เป็นของตัวเอง
  • เมื่อเกิด exception Java จะตรวจสอบ catch block ตามลำดับจนกว่าจะพบที่ตรงกัน
try {
    ArrayList<String> nums 
      = new ArrayList<String>();
    nums.add("one");
    String last = nums.get(0);
    Integer.valueOf(last);
    System.out.println(last);
} catch (IndexOutOfBoundsException eIndex) {
    System.out.println("Oops - wrong index");
} catch (NumberFormatException eValueOf) {
    System.out.println(
      "Oops - String not a number");
}
Oops - String not a number
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

Catch "All"

  • ใช้ catch (Exception variable){ } เพื่อดักจับทุก exception
    • จัดการ exception ที่ catch block อื่นไม่ได้จัดการไว้
try {
    // Code to try
} catch (IndexOutOfBoundsException eIndex) {
    System.out.println("Oops - wrong index");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops - String not a number");
} catch (Exception e) { // Handles all exceptions except the two above
    System.out.println("Something unplanned happened");
}
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

Finally

  • โค้ดใน finally block จะทำงานเสมอ
// When exception happens
try {
    Integer.valueOf("one");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops");
} finally {
    System.out.println("Doing cleanup");
}
Oops
Doing cleanup

 

// When no exception gets thrown
try {
    Integer.valueOf("1");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops");
} finally {
    System.out.println("Doing cleanup");
}
Doing cleanup
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

ออบเจกต์ Exception

  • ออบเจกต์ Exception ที่ส่งเข้า catch block มีข้อมูลมากมาย
  • ใช้เมธอดบนออบเจกต์เพื่อดูรายละเอียดของข้อผิดพลาด
    • .getMessage() เพื่อดูสาเหตุในรูปแบบที่อ่านได้
    • .getClass() เพื่อดูชื่อคลาสหรือประเภทของ Exception
    • .printStackTrace() เพื่อแสดง stack trace (สไลด์ถัดไป)
public class ExceptionExample {

    public static void main(String[] args) {
        try {
            Integer.valueOf("one");
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

Exception stack trace

  • stack trace บันทึกลำดับการทำงานของโปรแกรมจนถึงจุดที่เกิดปัญหา
    • ด้านล่าง: ผลลัพธ์จากโค้ดตัวอย่างก่อนหน้า รวมถึง stack trace ที่แสดงออกมา
For input string: "one"
java.lang.NumberFormatException: For input string: "one"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:661)
    at java.base/java.lang.Integer.valueOf(Integer.java:988)
    at foo/exceptions.ExceptionExample.main(ExceptionExample.java:7)
ชนิดข้อมูลและการจัดการข้อยกเว้นใน Java

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

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

Preparing Video For Download...