class x public static void mainString args System

  • Slides: 10
Download presentation

แบบไมตรวจจบ class x { public static void main(String[] args) { System. out. println(1/0); }

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

ตรวจจบแบบคลมหมด class x { public static void main(String[] args) { try { System. out.

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

ตรวจจบเฉพาะ Arithmetic. Exception class x { public static void main(String[] args) { try {

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

การใช throw class x { public static void main(String[] args) { try { int

การใช 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 } http: //www. thaiall. com/class Page: 6

การใช finally class x { public static void main(String[] args) { try { System.

การใช 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. Arithmetic. Exception: / by zero // printed both catch and not catch http: //www. thaiall. com/class Page: 7

การใช throws class x { public static void main(String[] args) { y z =

การใช 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. Arithmetic. Exception: / by zero http: //www. thaiall. com/class Page: 8

3 Catch with order of level class x { public static void main(String[] args)

3 Catch with order of level class x { public static void main(String[] args) { try { System. out. println(1/0); } catch (Arithmetic. Exception e) {System. out. print(e); } catch (Array. Index. Out. Of. Bounds. Exception e) {System. out. print(e); } catch (Index. Out. Of. Bounds. Exception e) { System. out. print(e); } catch (Runtime. Exception e) { System. out. print(e); } catch (Exception e) { System. out. print(e); } }} บรรทดสนำเงน ไมขนตอกน แตอยภายใต Java. lang. Exception บรรทดสเขยว ขนตอกน เพราะ Index. Out. Of. Bounds. Exception อยใน Runtime. Exception แลว ถาจบ Runtime. Exception กจะจบ Index. Out. Of. Bounds. Exception ไปดวย จบตามลำดบ ถาม Exception http: //www. thaiall. com/class Page: 9

�������� Exception ���� http: //docs. oracle. com/javase/1. 4. 2/docs/api/java/lang/Array. Index. Out. Of. Bounds. Exception.

�������� Exception ���� http: //docs. oracle. com/javase/1. 4. 2/docs/api/java/lang/Array. Index. Out. Of. Bounds. Exception. html http: //www. thaiall. com/class Page: 10