exception message Exception in thread main java lang

  • Slides: 25
Download presentation

ตวอยาง exception message ทระบบเตอนผใช Exception in thread "main" java. lang. Arithmetic. Exception: / by

ตวอยาง exception message ทระบบเตอนผใช Exception in thread "main" java. lang. Arithmetic. Exception: / by zero at Exception. Demo. main(Exception. Demo. jav a: 5) Exception. Demo : The class name main : The method name Exception. Demo. java : The filename java: 5 : Line number 6

ประเภทของ exceptions (Types of exceptions) ม 2 ประเภท 1)Checked exceptions 2)Unchecked exceptions 7

ประเภทของ exceptions (Types of exceptions) ม 2 ประเภท 1)Checked exceptions 2)Unchecked exceptions 7

โครงสรางของ exception (exception hierarchy) 10

โครงสรางของ exception (exception hierarchy) 10

การจดการขอผดพลาด (exception handling) ของภาษาจาวา • exception handling ของภาษาจาวามรปแบบการใชงาน ดงน try { <statements>; } catch

การจดการขอผดพลาด (exception handling) ของภาษาจาวา • exception handling ของภาษาจาวามรปแบบการใชงาน ดงน try { <statements>; } catch (Throwable 1 t) { <statements>; } catch (Throwable 2 t) { <statements>; } catch (Throwable 3 t) { <statements>; } 11

พมพ main() call exception activation stack หยดการทำงานของ JVM Exception Method 1 call Exception Method

พมพ main() call exception activation stack หยดการทำงานของ JVM Exception Method 1 call Exception Method 2 14

ดำเนนการตามทไดดกจบ Exception main() call Exception Method 1 call Method 2 15

ดำเนนการตามทไดดกจบ Exception main() call Exception Method 1 call Method 2 15

ตวอยาง 1. class Array. Out { 2. public static void main(String s []) {

ตวอยาง 1. class Array. Out { 2. public static void main(String s []) { 3. System. out. println("Hello " + s[0]); 4. } 5. } Exception in thread "main" java. lang. Array. Index. Out. Of. Bounds. Exception: 0 at Array. Out. main(Array. Out. java: 3) 16

การเพม exception handling 1. class Array. Out { 2. public static void main(String s

การเพม exception handling 1. class Array. Out { 2. public static void main(String s []) { 3. try 4. { System. out. println("Hello " + s[0]); } 5. catch (Array. Index. Out. Of. Bounds. Exception e) 6. { System. out. println("Please try again" + 7. "with command line"); } 8. } 9. } 17

ตวอยาง 1. class Test. Excep { 2. static void f(){int x = 0; float

ตวอยาง 1. class Test. Excep { 2. static void f(){int x = 0; float y = 1/x; } 3. public static void main(String s[]){ 4. try{ 5. f(); 6. }catch (Exception e){ 7. System. out. println("Error divice by 0" + e. get. Message()); } 8. } 9. } 18

ตวอยาง 1. class Throw. Test { 2. static int div(int x, int y){ 3.

ตวอยาง 1. class Throw. Test { 2. static int div(int x, int y){ 3. try { 4. if (y == 0) 5. throw new Exception(); 6. return x/y; 7. } catch (Exception e){return x/y; } 8. } 9. public static void main(String s[ ]){ 10. System. out. println(div(1, 0)); } 11. } 20

ตวอยาง 1. class Method. Throws { 2. static int div(int x, int y) throws

ตวอยาง 1. class Method. Throws { 2. static int div(int x, int y) throws Arithmetic. Exception { 3. return x/y; } 4. public static void main(String s[]){ 5. System. out. println(div(1, 0)); 6. } 7. } 22

ตวอยาง 1. import java. lang. * ; 2. import java. io. * ; 3.

ตวอยาง 1. import java. lang. * ; 2. import java. io. * ; 3. public class Square { 4. public static void main ( String[] a ) throws IOException { 5. Buffered. Reader stdin = new Buffered. Reader (new Input. Stream. Reader( System. in) ); 6. String in. Data; 7. int num ; 8. System. out. println("Enter an integer: "); 9. in. Data = stdin. read. Line(); 10. num = Integer. parse. Int( in. Data ); // convert in. Data to int 11. System. out. println("The square of " + in. Data + " is " 23 +

1. class Array. Out { 2. public static void main(String s []) { 3.

1. class Array. Out { 2. public static void main(String s []) { 3. try 4. { System. out. println("Hello " + s[0]); } 5. catch (Array. Index. Out. Of. Bounds. Exception e) 6. { System. out. println("Please try again" + 7. "with command line"); } 8. finally 9. {System. out. println("How are you. "); } 10. } 11. } 25