1 / 10

การโปรแกรมเชิงวัตถุด้วยภาษา JAVA

การโปรแกรมเชิงวัตถุด้วยภาษา JAVA. Exception. มหาวิทยาลัยเนชั่น http://www. nation. ac.th. บุรินทร์ รุจจนพันธุ์ . ปรับปรุง 6 มิถุนายน 255 6. ความหมาย. Exception เป็นชื่อคลาสอยู่ใน package java.lang คลาสนี้ใช้ตรวจจับข้อผิดพลาดของโปรแกรม

Download Presentation

การโปรแกรมเชิงวัตถุด้วยภาษา JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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. ตรวจจับแบบคลุมหมด 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/Exception.html

  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/ArithmeticException.html

  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. การใช้ 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. การใช้ 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. 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. ระดับการตรวจจับของ Exception สายหนึ่ง http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ArrayIndexOutOfBoundsException.html

More Related