งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 6 มิถุนายน 2556 Exception มหาวิทยาลัยเนชั่น

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 6 มิถุนายน 2556 Exception มหาวิทยาลัยเนชั่น"— ใบสำเนางานนำเสนอ:

1 http://www.thaiall.com/class Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 6 มิถุนายน 2556 Exception มหาวิทยาลัยเนชั่น http://www.nation.ac.th มหาวิทยาลัยเนชั่น http://www.nation.ac.th

2 http://www.thaiall.com/class Page: 2 ความหมาย Exception เป็นชื่อคลาสอยู่ใน package java.lang คลาสนี้ใช้ตรวจจับข้อผิดพลาดของโปรแกรม และดำเนินการกับเหตุการณ์ที่มักไม่ปกติ ในแบบข้อยกเว้น เช่น - ผลการดำเนินการทางคณิตศาสตร์ที่ปล่อยให้มีการ หารด้วย 0 - การเรียกใช้อาร์เรย์ ที่กำหนด index นอกขอบเขตที่ อาร์เรย์เก็บไว้ มีคำสำคัญ 4 คำ คือ try, catch, throw, finally

3 http://www.thaiall.com/class Page: 3 แบบไม่ตรวจจับ class x { public static void main(String[] args) { System.out.println(1/0); } Exception in thread "main" java.lang.ArithmeticException: / by zero at x.main(x.java:3)

4 http://www.thaiall.com/class Page: 4 ตรวจจับแบบคลุมหมด class x { public static void main(String[] args) { try { System.out.println(1/0); } catch (Exception e) { System.out.println(e); } // java.lang.ArithmeticException: / by zero } http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/E xception.html

5 http://www.thaiall.com/class Page: 5 ตรวจจับเฉพาะ ArithmeticException class x { public static void main(String[] args) { try { System.out.println(1/0); } catch (ArithmeticException e) { System.out.println(e); } // java.lang.ArithmeticException: / by zero } http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Ar ithmeticException.html

6 http://www.thaiall.com/class Page: 6 การใช้ throw class x { public static void main(String[] args) { try { int i = 0; if(i == 0){throw new Exception("nothing");} // unreachable "hello" System.out.println("hello"); System.out.println(1/i); }catch(Exception e) {System.out.println(e);} } // java.lang.Exception: nothing }

7 http://www.thaiall.com/class Page: 7 การใช้ finally class x { public static void main(String[] args) { try { System.out.println(1); System.out.println(1/0); System.out.println(2); } catch (Exception e) { System.out.println(e); } finally { System.out.println("printed both catch and not catch"); } }} // 1 // java.lang.ArithmeticException: / by zero // printed both catch and not catch

8 http://www.thaiall.com/class Page: 8 การใช้ throws class x { public static void main(String[] args) { y z = new y(); try { z.yy(); } catch (Exception e) {System.out.print(e);} }} class y { public static void yy() throws Exception { System.out.println(1); System.out.println(1/0); System.out.println(2); }} // 1 // java.lang.ArithmeticException: / by zero

9 http://www.thaiall.com/class Page: 9 3 Catch with order of level class x { public static void main(String[] args) { try { System.out.println(1/0); } catch (ArithmeticException e) {System.out.print(e);} catch (ArrayIndexOutOfBoundsException e) {System.out.print(e);} catch (IndexOutOfBoundsException e) { System.out.print(e); } catch (RuntimeException e) { System.out.print(e); } catch (Exception e) { System.out.print(e); } }} บรรทัดสีน้ำเงิน ไม่ขึ้นต่อกัน แต่อยู่ภายใต้ Java.lang.Exception บรรทัดสีเขียว ขึ้นต่อกัน เพราะ IndexOutOfBoundsException อยู่ใน RuntimeException แล้ว ถ้าจับ RuntimeException ก็จะจับ IndexOutOfBoundsException ไปด้วย จับตามลำดับ ถ้ามี Exception ต้องไปอยู่รายการสุดท้ายของการ จับ จับซ้ำไม่ได้ จับได้รอบเดียวแต่ละการจับ

10 http://www.thaiall.com/class Page: 10 ระดับการตรวจจับของ Exception สายหนึ่ง http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ArrayIndex OutOfBoundsException.html


ดาวน์โหลด ppt Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 6 มิถุนายน 2556 Exception มหาวิทยาลัยเนชั่น

งานนำเสนอที่คล้ายกัน


Ads by Google