SlideShare a Scribd company logo
1 of 26
Download to read offline
การดักจับขอผิดพลาด
Exception Handling
วัตถุประสงค
♦ เพื่อใหผูเรียนเขาใจหลักการทํางานการดักจับขอผิดพลาดของการเขียนโปรแกรมเชิงวัตถุ
♦ เพื่อใหผูเรียนสามารถนําหลักการทํางานการดักจับขอผิดพลาดของการเขียนโปรแกรมเชิง
วัตถุไปประยุกตใชเขียนโปรแกรมจริงได
บทที่
11
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
245
เนื้อหาบทเรียน
♦ การดักจับขอผิดพลาด
♦ คําสั่ง try
♦ คําสั่ง catch
♦ คําสั่ง final
♦ คําสั่ง throw, throws
♦ การสรางคลาสตรวจจับขอผิดพลาดใชเอง
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
246
ความผิดปกติ(Exception)
Exception คือ เหตุการณผิดปกติที่เกิดขึ้น สงผลใหการทํางานของโปรแกรมไมเปนไปตาม
ขั้นตอนที่ไดกําหนดไว ตัวอยางของการเกิด Exception เชน เมื่ออางถึงตัวแปรอารเรยเกินขนาดที่
กําหนด หรือการหารดวยตัวเลข 0 เปนตน โดย exception แบงออกเปนหลายประเภท ซึ่ง exception
แตละประเภทจะเปนคลาสลูกของ คลาส Exception ดังรูปตอไปนี้
ตัวอยาง Exception ที่เกิดขึ้นขณะรันโปรแกรม โดยโปรแกรมีการ / ดวย 0
public class Error1 {
public static void main(String[] args) {
int x = 5 / 0;
System.out.println("Result = " + x);
}
}
โปรแกรมนี้เมื่อคอมไพลจะไมเกิดขอผิดพลาด
ผลลัพธที่ไดจากการรันโปรแกรม
java.lang.ArithmeticException: / by zero at Error1.main(Error1.java:3)
Exception in thread "main"
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
247
ตัวอยาง การเกิด Exception ขณะ array เกินขนาดที่จองไว
public class Error2 {
public static void main(String[] args) {
int x[] = {0,1,2};
for (int i =0; i <= x.length ; i++ ) {
System.out.println("Result = " + x[i]);
}
System.out.println(“Good bye”);
}
}
เมื่อผานการคอมไพล ผลลัพธที่ไดจากการรันโปรแกรมคือ
Result = 0
Result = 1
Result = 2
java.lang.ArrayIndexOutOfBoundsException: 3
at Error2.main(Error2.java:6)
Exception in thread "main"
จากการรันโปรแกรมนี้จะพบวาหากโปรแกรมไมเกิดขอผิดพลาดกอน จะพิมพขอความวา Good
bye ออกบนหนาจอ แตโปรแกรมนี้ พิมพขอความ result = 0 result =1 result =2 แลวก็หยุดไป
พรอมกับแสดงขอความ java.lang.ArrayIndexOutOfBoundsException: 3 แสดงวาโปรแกรมเกิด
ขอผิดพลาดขณะรันโปรแกรม(runtime exception) ที่ชื่อวา ArrayIndexOutOfBoundsException ขึ้นกอน
ทําใหโปรแกรมหยุดการทํางานไป
การเกิด Exception ใน Java
o เมื่อเกิด Exception ขึ้นในภาษา Java ตัว JVM จะสรางออปเจ็คในตระกลูของ Throwable Class
ขึ้นมาแลวโยนออกมา
o เมื่อ JVM โยนออปเจ็ค Exception ออกมาเราสามารถดักจับดวยคําสั่ง try, catch, finally แตถา
เราไมจัดการ JVM ก็จะเปนคนจัดการเองคือแสดงการเกิด Exception และก็ลุดออกโปรแกรมดัง
ตัวอยางขางตน
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
248
Exception Hierarchy
จะแบงออกเปน 3 กลุมดวยกัน
►จะแบงออกเปน 3 กลุมดวยกัน
Error เปน Exception ที่เกิดขึ้นจาก JVM เองไมเกี่ยวกับการโปรแกรมของเรา เชน
StackOverflowError, OutOfMemoryError
RuntimeException เปน Exception ที่เกิดจากการโปรแกรมของเราซึ่งถาเกิด Exception
ในตระกลูนี้ควรจะแกไขโปรแกรมโดยไมใช try – catch - finally เชน
ArithmeticException, NullPointerException, IndexOutOfBoundsException
Exception เปน Exception ที่จะตองดักจับเชน FileNotFoundException, EOFException
วิธีการจัดการกับ Exception
Exception สามารถเกิดขึ้นไดกับโปรแกรมเสมอ ทุกที่ทุกเวลา ดังนั้นเพื่อใหโปรแกรมของเรา
ทํางานจบอยางปกติแมวาจะเกิดขอผิดพลาดเหลานั้น โดยเราจะตองจัดการกับขอผิดพลาด(Exception
Handing) ที่อาจเกิดขึ้นในโปรแกรม โดยระบุการดักจับ exception ไวในสวนที่คาดวาจะเกิดขอผิดพลาด
และทําการจัดการกับ exception ดังกลาว ดวยคําสั่ง Try ,catch ,throw หรือ finally
คําสั่ง try, catch and finally
ใชในการดักจับ Exception ที่เกิดขึ้นจากการเรียกใช Methods หรือ คําสั่ง เมื่อเราจับ Exception
ที่เกิดขึ้นแลว Exception ก็จะหายไปโปรแกรมก็จะทํางานตอโดยขามคําสั่งในบล็อกไป
Error Exception
RuntimeException
Object
Throwable
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
249
ขอกําหนด
เราจะนํา code ที่เราคิดวาอาจจะเกิดขอผิดผลาดมาไวในบล็อก try ถาเกิดขอผิดผลาดหรือ
Exception ก็จะถูกจับโดยบล็อกของ catch
หลักการทํางาน
เมื่อเกิดขอผิดผลาดในบล็อกของ try JVM ก็จะโยน Exceptin มาใหกับบล็อกของ catch
ตัวอยาง การใชดักจับขอผิดพลาดที่อาจเกิดขึ้น
class Zero{
public static void main(String args[]) {
int numerator = 10;
int denominator = 0;
System.out.println("This test devide by zero");
try {
System.out.println(numerator/denominator);
}
catch(ArithmeticException e) {
System.out.println("Arithemetic Exception");
System.out.println("Cann't compute");
}
System.out.println("This text will not be printed.");
}
}
ผลลัพธที่ไดคือ
This test devide by zero
Arithemetic Exception
Cann't compute
This text will not be printed.
รูปแบบการใชงาน try และ catch
try{
[statements]
}
[catch( ExceptionType1 ExceptionName1) {
[statements]
}]
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
250
ตัวอยาง การใชดักจับขอผิดพลาดที่อาจเกิดขึ้น
class Zero{
public static void main(String args[]) {
int numerator = 10;
int denominator = 0;
System.out.println("This test devide by zero");
try {
System.out.println(numerator/denominator);
}
catch(Exception e) {
System.out.println(e.toString());
}
System.out.println("This text will not be printed.");
}
}
ผลลัพธที่ไดคือ
This test devide by zero
Arithemetic Exception java.lang.ArithmeticException: / by zero
This text will not be printed.
ตัวอยาง การใชดักจับขอผิดพลาดที่อาจเกิดขึ้น
public class Error2 {
public static void main(String[] args) {
int x[] = {0,1,2};
try{
for (int i =0; i <= x.length ; i++ )
System.out.println("Result = " + x[i]);
}
catch (Exception e){
System.out.println("GO GOOOO");
}
}
}
ผลลัพธที่ไดคือ
Result = 0
Result = 1
Result = 2
GO GOOOO
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
251
ตัวอยาง การใชดักจับขอผิดพลาดที่ไมถูกตอง
try {
System.out.println(“in of try block”);
}
System.out.println(“out of try block”); // ขาด block try
ตัวอยาง การใชดักจับขอผิดพลาดที่อาจเกิดขึ้น
try {
System.out.println(“in of try block”);
}
System.out.println(“out of try block”); // error เพราะ try ตองตามดวย catch ทันที
catch ( Exception e ) {
System.out.println(“in of catch block”);
}
ขอกําหนด
- ถามี catch บล็อกหลายตัว ExceptionType จะตองไมซ้ํากันและจะตองจัดเรียงตาม Hierarchy
ดวย โดย Hierarchy ที่ต่ํากวาจะอยูดานบน Hierarchy ที่สุดกวาจะอยูดานลาง
หลักการทํางาน
พิจารณา Exception วา ExceptionType ที่โยนมานั้น ตรงหรือใกลเคียงกับ ExceptionType ใด
ที่สุดในบล็อกของ catch
รูปแบบการใช Try คูกับ หลาย Catch
try {
<statements>
}
catch (<ExceptionType1> <identifier>) {
<statements>
}
catch(<ExceptionType2> <identifier>) {
<statements>
}
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
252
ตัวอยางคลาส Exception มาตราฐาน เชน IOException , NoSuchMethodException ,
FileNotFoundExceptionฯ ซึ่งคลาสเหลานี้ตองทําการ import java.io.IOException; หรืออื่นๆ ตามลําดับ
ถาไมรูจะตองดักจับ Exception อะไรบาง.... ทําอยางไรดี
การดักจับ exception ไมใชเรื่องยากก็จริง แตเรื่องที่ยากคือ การที่เราไมรูวามี exception
อะไรบางที่จะตองดักจับ ตรงนี้หากเขียนโปรแกรมไปสักพัก มีความชํานาญมากพอ แลวคงจะทําให
ทราบไดเองจากประสบการณวาจะตองดักจับ exception ประเภทใดบาง แต ณ ตอนนี้หากไมทราบจริงๆ
วาจะตองดักจับ exception อะไรบาง วิธีที่งายที่สุด คือ ใหดักจับ exception ประเภท Exception เพราะ
exception ทุกประเภทที่เกิดขึ้นในโปรแกรม ลวนเปน ออบเจ็คของคลาสที่สืบทอดคุณสมบัติมาจาก
คลาส Exception ทั้งสิ้น(จะสืบทอดโดยตรงหรือไม ไมใชเรื่องสําคัญ) ดังนั้นไมวาจะเกิด exception
ประเภทใดขึ้นหากระบุใหดักจับ exception ประเภท Exception แลวจะสามารถดักจับไดทั้งหมด
ตัวอยาง การดักจับขอผิดพลาด
class testException{
public static void main(String[] args) {
int x[] = {1, 10, 22};
for(int i = 0; i <= 3; i++)
try{
System.out.println(x[i] / i );
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error!!! array out of bound");
}
catch(ArithmeticException e){
System.out.println("Error!!! divided by ZERO");
}
catch(Exception e){
System.out.println("Error!!! Something...");
}
System.out.println("Bye Bye");
}}
ผลลัพธที่ไดคือ
Error!!! divided by ZERO
10
11
Error!!! array out of bound
Bye Bye
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
253
ตัวอยาง การดักจับขอผิดพลาด
import javax.swing.JOptionPane;
class DivByZero{
public static void main(String args[]){
String s1, s2;
int i1 , i2;
try{
s1 = JOptionPane.showInputDialog("Enter number 1 : ");
s2 = JOptionPane.showInputDialog("Enter number 2 : ");
i1 = Integer.parseInt(s1);
i2 = Integer.parseInt(s2);
JOptionPane.showMessageDialog(null, Integer.toString(i1 / i2));
}
catch (NumberFormatException e){//1
JOptionPane.showMessageDialog(null,e.getMessage());
System.exit(0);
}
catch(ArithmeticException e){//2
JOptionPane.showMessageDialog(null,e.getMessage());
System.exit(0);
}
catch(Exception e){//3
JOptionPane.showMessageDialog(null,e.getMessage());
System.exit(0);
}
System.exit(0);
}
}
จากโปรแกรมจะทําการแสดง Dialog box เพื่อรับตัวเลขจํานวน 2 ครั้ง แลวนําตัวแรกเปนตัวตั้ง
แลวเอาคาที่สองเปนตัวหาร ซึ่งโปรแกรมไดทําการดักจับ error ไว 2 รูปแบบและหากเกิดขอผิดพลาด
จากนั้นจะกระทําใน Exception ที่ 3
ตัวอยางที่ผิด
try {
RandomAccessFile raf = new RandomAccessFile(“myfile.txt”, “r”);
byte b[] = new byte[1000];
raf.readFully(b, 0, 1000);
} catch ( IOException e ) {
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
254
System.out.println(“IO Error”);
} catch ( FileNotFoundException e ) {
System.out.println(“File not found”);
}
ผิดเนื่องจาก FileNotFoundException เปน sup class ของ IOException ดังนี้
ใหถูกตองเปลี่ยนเปน
try {
RandomAccessFile raf = new RandomAccessFile(“myfile.txt”, “r”);
byte b[] = new byte[1000];
raf.readFully(b, 0, 1000);
} catch ( FileNotFoundException e ) {
System.out.println(“File not found”);
} catch ( IOException e ) {
System.out.println(“IO Error”);
}
การใช try-catch block อยางปกติ จะทําใหการดําเนินของโปรแกรมผานเขามาที่จุดเดียวและผาน
ออกไปที่จุดเดียว แตหากใน try block หรือ catch block มีการใชประโยคควบคุมที่ทําใหการดําเนินของ
โปรแกรมกระโดดออกไปจากเสนทางปกติ เชน break , continue ,หรือ return ซึ่งไมสามารถคาดเดาได
ลวงหนาวาโปรแกรมจะดําเนินออกไปจาก try-catch ที่จุดใด ในลักษณะเชนนี้ถามีโปรแกรมที่ตอง
ทํางานหลังจากจบ try-catch block นั้น เชน การปดไฟลหรือ ปด socket หรือการคืนหนวยความจําของ
graphic หากนําโปรแกรมเหลานี้มาไวตอจาก catch block ก็จะทําใหโปรแกรมเหลานี้ไมถูกทํางานในบาง
กรณี ดังนั้นภาษาจาวา จึงมี finally block ไวสําหรับใหโปรแกรมที่ตองถูกทํางานเสมอไมวาโปรแกรม
จะดําเนินออกจาก try-catch block ในเสนทางใด
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
255
ขอกําหนด
- ถามีบล็อก try จําเปนตองมีบล็อก catch อยางนอย 1 บล็อก หรือถามีมีบล็อก catch ก็ตองมี
บล็อก finally
หลักการทํางาน
- เมื่อสิ้นสุดการทํางานบล็อกของ try…catch แลว ไมวาจะเกิด Exception หรือไมก็จะตองเขามา
ทํางานในบล็อกของ finally ทุกครั้ง
- ถึงแมจะสั่งคําสั่ง return หรือ System.exit() ใน try บล็อก หรือ catch บล็อก ก็จะตองเขามา
ทํางานใน Finally บล็อกทุกครั้ง
ตัวอยาง การเขียนโปรแกรมที่มีการดักจับ โดยใช Try – catch – finally
public class Error4 {
public static void main(String[] args) {
try{int x = 5 / 0;
System.out.println("Result = " + x);
}
catch(ArithmeticException a){
System.out.println("Arithmeitc Error");
}
finally{
System.out.println("Finally Print");
}
}}
รูปแบบการทํางาน Try , catch , finally
try {
……..
} catch (IOException e) {
……..
} catch (Exception e) {
……..
} finally {
//statements placed here will always
//get executed
} //// ffiinnaallllyy จะทํางานทุกครั้งไมวาจะเกิดจะทํางานทุกครั้งไมวาจะเกิด eexxcceeppttiioonn ขึ้นหรือไมก็ตามขึ้นหรือไมก็ตาม
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
256
ผลลัพธที่ไดคือ
Arithmeitc Error
Finally Print
ตัวอยาง การเขียนโปรแกรมที่มีการดักจับ โดยใช Try – finally
public class Error4 {
public static void main(String[] args) {
try{
int x = 5 / 0;
System.out.println("Result = " + x);
}
finally{
System.out.println("Finally");
}
}
}
ผลลัพธที่ไดคือ
Finally
java.lang.ArithmeticException: / by zero
at Error3.main(Error3.java:3)
Exception in thread "main"
ตัวอยาง โปรแกรมที่ทําการดักจับขอผิดพลาดโดยใช try catch finally
String s = null;
try {
int x = s.length();
System.out.println( x );
} catch ( Exception e ) { // Exception e = Exception
System.out.println(“Catch”);
} finally {
System.out.println(“Finally”);
}
ผลลัพธคือ
Catch
Finally
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
257
ตัวอยาง ที่ผิดไมควรเขียนบล็อก finally กอน catch
String s = null;
try {
int x = s.length();
System.out.println( x );
} finally {
System.out.println(“Finally”);
} catch ( Exception e ) {
System.out.println(“Catch”);
}
ผลลัพธที่ไดคือ : Compiled Error
ไมสามารถเขียนบล็อก finally ไวกอนบล็อก catch
ตัวอยาง การตรวจสอบ Null
String s = null;
try {
int x = s.length();
System.out.println( x );
} finally {
System.out.println(“Finally”);
}
ผลลัพธคือ
Finally
Exceptoin in thread “method” java.lang.NullPointException
at ClassName.method(FileName.java:15)
ตัวอยาง การตรวจจับขอผิดพลาด ดวย try catch finally
class testException3{
public static void main(String[] args) {
int x[] = {1, 10, 22};
try{
for(int i = 0; i <= 3; i++)
System.out.println(x[i] / i );
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error!!! array out of bound");
}
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
258
catch(ArithmeticException e){
System.out.println("Error!!! divided by ZERO");
}
catch(Exception e){
System.out.println("Error!!! Something...");
}
finally{
System.out.println("finally print");
}
System.out.println("Bye Bye");
}
}
ผลลัพธที่ไดคือ
Error!!! divided by ZERO
finally print
Bye Bye
ตัวอยาง การตรวจจับขอผิดพลาด ดวย try catch finally
class testException2{
public static void main(String[] args) {
int x[] = {1, 10, 22};
for(int i = 0; i <= 3; i++)
try{
System.out.println(x[i] / i );
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error!!! array out of bound");
}
catch(ArithmeticException e){
System.out.println("Error!!! divided by ZERO");
}
catch(Exception e){
System.out.println("Error!!! Something...");
}
finally{
System.out.println("finally print");
}
System.out.println("Bye Bye");
}}
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
259
ผลลัพธที่ไดคือ
Error!!! divided by ZERO
finally print
10
finally print
11
finally print
Error!!! array out of bound
finally print
Bye Bye
การจัดการ Exception ที่เกิดใน Method
สามารถทําได 2 แบบ
o ใช try…catch…finally ดักจับ Exception ใน Methods เลย(แบบฝกหัดที่ 2)
o โยน Exception ออกใหกับผูเรียก(Method)ใช โดยใช throw กับ throws
Throws Exception
หากใน method ใดมีประโยคที่อาจจะมีการสง exception ออกมา แตไมมีการดักจับและจัดการ
กับ exception นั้น เราจะตองระบุให method นั้นเปน method ที่จะสง exception ออกมาโดยใช keyword
‘throws’ กํากับไวหลังจากวงเล็บของพารามิเตอร และตามดวยคลาสของ exception ที่จะถูกสงออกมา
การเรียกใช method ที่ถูกระบุวาจะสง exception ออกมาจะตองมีการดักจับและจัดการกับ exception นั้น
มิเชนนั้นจะคอมไพลไมผาน
หากมี exception เกิดขึ้นในเมธอดหนึ่ง แตไมถูกดักจับภายในเมธอดนั้น จะทําให exception ถูก
สงตอ(propagate) ใหกับเมธอดที่เรียกเมธอดนั้นใหทํางาน เชน method main เรียก method A ใหทํางาน
และเมธอด A เรียก method B ใหทํางาน หากเกิด exception ขึ้นใน method B แต method B ไมไดดักจับ
exception นั้น โปรแกรมจะสง exception ไปให method A ตอไป และถา method A ไมดักจับ
exception นั้นอีก method main ซึ่งเปนผูเรียก method A ใหทํางานก็จะตองดักจับและจัดการกับ
exception นั้น ซึ่งเมธอด main จะหลีกเลี่ยงการดักจับ exception ไมไดแลว เพราะหากยังไมมีการดักจับ
exception ที่ method main อีก จะสงผลใหโปรแกรมจบการทํางานลงแบบไมสมบูรณ
สรุป
- catch คือ การดักจับ exception วาเปนของเหตุการณใดและจัดการกับเหตุการณนั้น
- throw คือ หากเกิดขอผิดพลาดบางอยาง exception ถูกโยนออกจากโปรแกรม
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
260
รูปแบบการใช Throws
กรณีที่ 1 : การเกิด exception(การโยน exception)จาก method
[Modifier] return_type method_name([argumentsล) throws ExceptionType [, ExceptionType,…] {
[statements]
throw exception_object
[statements]
}
กรณีที่ 2 : หาก exception ไมไดรับการดักจับภายใน method ที่โยน exception นั้นออกมา method ที่เปน
ผูเรียกเมธอดนั้นใหทํางานจะตองเลือกระหวางการดักจับ exception หรือ สงตอ exception ใหกับเมธอด
ที่เรียกใชงาน อยางใดอยางหนึ่ง ซึ่งในการสงตอ exception จะตองประกาศ method ดังนี้
[Modifier] return_type method_name([argumentsล) throws ExceptionType [, ExceptionType,…] {
[statements]
}
เปนการประกาศวา method นั้นอาจจะโยนออปเจ็ค Exception ประเภทนั้นๆ(ExceptionType)
ออกไป และสามารถมี ExceptionType ไดหลายตัวแตไมซ้ํากัน
ตัวอยาง การประกาศ Throws
void method1() throws NullPointerException, IOException {
.
.
.
}
คําสั่ง Throws
รูปแบบ
throw ExceptionObject;
เปนการสั่งให JVM โยนออปเจ็ค Exception ที่เราระบุออกไปโดยไมไดเกิดขอผิดผลาดใดๆ
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
261
ตัวอยาง การใชคําสั่ง throw
if ( x == 0 ) {
throw new ArithmeticException();
} else {
return 5 / x;
}
ตัวอยาง การใช throw และ throws รวมกัน
- ตัวอยางนี้เกิด Compiled Error
void method1() {
throw new FileNotFoundException();
}
- ที่ถูกตองเปน
void method1() throws FileNotFoundException {
throw new FileNotFoundException();
}
- หรือที่ถูกตองเปน
void method1() throws IOException {
throw new FileNotFoundException();
}
ตัวอยาง การจับ Exception ที่เราสรางขึ้น
- ตัวอยางที่ผิด
void method1() throws IOException {
throw new FileNotFoundException();
}
void method2() {
try {
method1();
} catch (FileNotFoundException e) {
}
}
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
262
- ที่ถูกตองเปน
void method2() {
try {
method1();
} catch (IOException e) {
}
}
ผลของการประกาศ throws
- เมื่อนํา Method นั้นไปใชจะตองใชคําสั่ง try…catch คอยดักจับ Exception ในชนิดนั้นๆ
- เราสามารถใชชนิดของ Exception ที่มี Hierarchy ที่สูงกวามารับ
ตัวอยางดักจับ Method ที่ประกาศ throws
void method1() throws IOException {
throw new FileNotFoundException();
}
void method2() {
try {
method1();
} catch (IOException e) {
System.out.println(“IOException”);
}
}
ผลลัพธที่ไดคือ
IOException
ตัวอยางดักจับ Method ที่ประกาศ throws
void method1() throws IOException {
throw new FileNotFoundException();
}
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
263
void method2() {
try {
method1();
} catch (FileNotFoundException e) {
Sytem.out.println(“FileNotFoundException”);
} catch (IOException e) {
System.out.println(“IOException e”);
}
}
ผลลัพธที่ไดคือ
FileNotFoundException
ตัวอยาง throw และ throws เมื่อ method 2 เรียกใช method 1 แลว method 1 ได throws exception ทํา
ให method ที่เรียกใชงานตอง thows exception เดียวกัน หรือ ซุปเปอรคลาสของ exception นั้น
- การประกาศ method ที่มีการ throws แลวเกิด ขอผิดพลาด compile error
void method1() throws IOException {
throw new FileNotFoundException();
}
void method2() {
method1();
}
- ที่ถูกตองเปน
void method1() throws IOException {
throw new FileNotFoundException();
}
void method2() throws IOException {
method1();
}
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
264
ตัวอยาง การ throws ออกจาก method
- ทําการคอมไพลแลวเกิด compile error
void method1() {
throw new FileNotFoundException();
}
- ที่ถูกตองเปน
void method1() throws FileNotFoundException {
throw new FileNotFoundException();
}
- หรือ ที่ถูกตองเปน
void method1() throws IOException {
throw new FileNotFoundException();
}
ตัวอยาง การThrows ออกจาก method
class testThrows{
static int div(int x , int y) throws ArithmeticException {
return (x/y);
}
public static void main(String args[]){
try {
System.out.println(div(15,0));
}
catch (ArithmeticException e){
System.out.println(e.toString());
System.out.println(e.getMessage());
}
}}
ผลลัพธที่ไดคือ
java.lang.ArithmeticException: / by zero
/ by zero
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
265
ตัวอยาง การ throw exception ออกจาก method
class testThrow1{
static int div(int x, int y){
try{
if(y==0)
throw new ArithmeticException();
return x/y;
}
catch (ArithmeticException e){
return Integer.MAX_VALUE; // 2147483647
}
}
public static void main(String[] args) {
System.out.println(div(1,0));
}
}
ผลลัพธที่ไดคือ
2147483647
ตัวอยาง
class ThrowException{
void method1(double a, double b){
try{
System.out.println(method2(a,b));
}
catch(ArithmeticException ex){
System.out.println(ex.getMessage());
}
}
String method2(double a, double b) throws ArithmeticException{
if (b==0){
System.out.println(a + "/" + b + "==>");
throw new ArithmeticException();
}
else
return a + "/" + b + "=" + a / b;
}
public static void main(String args[]){
ThrowException t = new ThrowException();
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
266
t.method1(20.0,3.0);
t.method1(20.0,0.0);
t.method1(20.0,5.0);
}
}
ผลลัพธคือ
20.0/3.0=6.666666666666667
20.0/0.0==>
null
20.0/5.0=4.0
ตัวอยาง throw และ throws
public class Test {
public static void aMethod() throws Exception {
try {
throw new Exception();
} finally {
System.out.println(“finally”);
}
}
public static void main(String args[]) {
try {
aMethod();
} catch (Exception e) {
System.out.println(“exception”);
}
System.out.println(“finished”);
}
}
ผลลัพธที่ไดคือ
finally
exception
finished
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
267
Exception Classes ของเราเอง
เราสามารถที่จะสืบทอด Exception Class ใชเปนชนิดของเราเองได
Excection Class ที่เราจะสืบทอดเพื่อนํามาใชควรเปนในตระกลู Exception ไมควรใช Error หรือ
RuntimeException
ตัวอยาง การสรางคลาส Exception ใชงานเอง
class MyException extends Exception { }
public class Test {
public static void aMethod() throws MyException {
throw new MyException();
}
public static void main(String args[]) {
try {
aMethod();
} catch (MyException e) {
System.out.println(“MyException”);
} catch (Exception e) {
System.out.println(“Exception”);
}
}
}
ผลลัพธที่ไดคือ
MyException
ตัวอยาง การสรางคลาส Exception ใชงานเอง
class MyException extends Exception{
MyException(String s){
super(s);
}
}
public class ExcepScope{
static void sayHello(String s) throws MyException{
if (s.equals("John"))
throw new MyException("Bad Guy");
System.out.println("Hello !" + s);
}
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
268
public static void main(String args[]){
String n[] = {"Joe" , "Jack" , "John" ,"Jim"};
for(int i = 0 ; i < n.length ; i++){
try{
sayHello(n[i]);
}
catch (MyException e){
System.out.println("No hello for a bad guy");
}
}
}}
ผลลัพธที่ไดคือ
Hello !Joe
Hello !Jack
No hello for a bad guy
Hello !Jim
บทที่ 11 การดักจับขอผิดพลาด หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
269
แบบฝกหัด
1. ใหนิสิตทําการดักจับขอผิดพลาดที่เกิดในโปรแกรมนี้
public class Error1 {
public static void main(String[] args) {
int x = 5 / 0;
System.out.println("Result = " + x);
}
}
สิ่งที่ไดขณะ run โปรแกรมคือ
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Error1.main(Error1.java:3)
2. จากโปรแกรมตอไปนี้
class testFinally1{
public static void main(String args[]){
String input ;
input = "Madee" ; // **TEST***
try{
if ((input.equals("Madee")))
return;
System.out.println("Hello " + input);
}
catch (Exception e){
System.out.println("Hello , whoever you are");
}
finally {
System.out.println("How are you today?");
}
System.out.println("It's nice to see you");
}
}
จากโปแกรม testFinally1 ทําการทดลองเปลี่ยนแปลงคาในบรรทัด //** TEST *** 3 กรณีดังตอไปนี้
1 . input = null;
2. input = “Joe“
3. input = "Madee" ; ผลลัพธที่ไดทั้งสามกรณีคืออะไร พรอมทั้งอธิบายเหตุผล

More Related Content

Viewers also liked

บทที่ 2 ชนิดของตัว
บทที่ 2 ชนิดของตัวบทที่ 2 ชนิดของตัว
บทที่ 2 ชนิดของตัวTheeravaj Tum
 
Javacentrix com chap11-1
Javacentrix com chap11-1Javacentrix com chap11-1
Javacentrix com chap11-1Theeravaj Tum
 
บทที่ 7 แพ็คเกจ
บทที่ 7 แพ็คเกจบทที่ 7 แพ็คเกจ
บทที่ 7 แพ็คเกจTheeravaj Tum
 
Javacentrix com chap04-0
Javacentrix com chap04-0Javacentrix com chap04-0
Javacentrix com chap04-0Theeravaj Tum
 
Javacentrix com chap05-0
Javacentrix com chap05-0Javacentrix com chap05-0
Javacentrix com chap05-0Theeravaj Tum
 
บทที่ 13 การดักจับเ
บทที่ 13 การดักจับเบทที่ 13 การดักจับเ
บทที่ 13 การดักจับเTheeravaj Tum
 
บทที่ 9 การพ้องรูป
บทที่ 9 การพ้องรูปบทที่ 9 การพ้องรูป
บทที่ 9 การพ้องรูปTheeravaj Tum
 
บทที่ 5 คลาส
บทที่ 5 คลาสบทที่ 5 คลาส
บทที่ 5 คลาสTheeravaj Tum
 
Javacentrix com chap01-0
Javacentrix com chap01-0Javacentrix com chap01-0
Javacentrix com chap01-0Theeravaj Tum
 
Javacentrix com chap11-2
Javacentrix com chap11-2Javacentrix com chap11-2
Javacentrix com chap11-2Theeravaj Tum
 
Projets sur les carrières
Projets sur les carrièresProjets sur les carrières
Projets sur les carrièresguimondghislain
 
Fake it ('till you make it)
Fake it ('till you make it)Fake it ('till you make it)
Fake it ('till you make it)Rémi Delhaye
 
[FRENCH] - Neo4j and Cypher - Remi Delhaye
[FRENCH] - Neo4j and Cypher - Remi Delhaye[FRENCH] - Neo4j and Cypher - Remi Delhaye
[FRENCH] - Neo4j and Cypher - Remi DelhayeRémi Delhaye
 
Presentation1
Presentation1Presentation1
Presentation1ashokkol
 
Etude cas maroc vs-infostratégique (2)
Etude cas maroc vs-infostratégique (2)Etude cas maroc vs-infostratégique (2)
Etude cas maroc vs-infostratégique (2)Chouaib Yousfi
 

Viewers also liked (18)

บทที่ 2 ชนิดของตัว
บทที่ 2 ชนิดของตัวบทที่ 2 ชนิดของตัว
บทที่ 2 ชนิดของตัว
 
Javacentrix com chap11-1
Javacentrix com chap11-1Javacentrix com chap11-1
Javacentrix com chap11-1
 
บทที่ 7 แพ็คเกจ
บทที่ 7 แพ็คเกจบทที่ 7 แพ็คเกจ
บทที่ 7 แพ็คเกจ
 
Javacentrix com chap04-0
Javacentrix com chap04-0Javacentrix com chap04-0
Javacentrix com chap04-0
 
Javacentrix com chap05-0
Javacentrix com chap05-0Javacentrix com chap05-0
Javacentrix com chap05-0
 
บทที่ 13 การดักจับเ
บทที่ 13 การดักจับเบทที่ 13 การดักจับเ
บทที่ 13 การดักจับเ
 
บทที่ 9 การพ้องรูป
บทที่ 9 การพ้องรูปบทที่ 9 การพ้องรูป
บทที่ 9 การพ้องรูป
 
บทที่ 5 คลาส
บทที่ 5 คลาสบทที่ 5 คลาส
บทที่ 5 คลาส
 
Javacentrix com chap01-0
Javacentrix com chap01-0Javacentrix com chap01-0
Javacentrix com chap01-0
 
Javacentrix com chap11-2
Javacentrix com chap11-2Javacentrix com chap11-2
Javacentrix com chap11-2
 
Projets sur les carrières
Projets sur les carrièresProjets sur les carrières
Projets sur les carrières
 
Fake it ('till you make it)
Fake it ('till you make it)Fake it ('till you make it)
Fake it ('till you make it)
 
[FRENCH] - Neo4j and Cypher - Remi Delhaye
[FRENCH] - Neo4j and Cypher - Remi Delhaye[FRENCH] - Neo4j and Cypher - Remi Delhaye
[FRENCH] - Neo4j and Cypher - Remi Delhaye
 
Presentation1
Presentation1Presentation1
Presentation1
 
Rupeee vs dollar
Rupeee vs dollarRupeee vs dollar
Rupeee vs dollar
 
Etude cas maroc vs-infostratégique (2)
Etude cas maroc vs-infostratégique (2)Etude cas maroc vs-infostratégique (2)
Etude cas maroc vs-infostratégique (2)
 
Demutualization of stock exchanges
Demutualization of stock exchanges Demutualization of stock exchanges
Demutualization of stock exchanges
 
Support de cours
Support de coursSupport de cours
Support de cours
 

More from Theeravaj Tum

Javacentrix com chap10-0
Javacentrix com chap10-0Javacentrix com chap10-0
Javacentrix com chap10-0Theeravaj Tum
 
Javacentrix com chap08-0
Javacentrix com chap08-0Javacentrix com chap08-0
Javacentrix com chap08-0Theeravaj Tum
 
Javacentrix com chap06-0
Javacentrix com chap06-0Javacentrix com chap06-0
Javacentrix com chap06-0Theeravaj Tum
 
Javacentrix com chap02-0
Javacentrix com chap02-0Javacentrix com chap02-0
Javacentrix com chap02-0Theeravaj Tum
 
บทที่ 12 กราฟฟิก
บทที่ 12 กราฟฟิกบทที่ 12 กราฟฟิก
บทที่ 12 กราฟฟิกTheeravaj Tum
 
บทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริบทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริTheeravaj Tum
 
บทที่ 8 คุณสมบัติก
บทที่ 8 คุณสมบัติกบทที่ 8 คุณสมบัติก
บทที่ 8 คุณสมบัติกTheeravaj Tum
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์Theeravaj Tum
 
บทที่ 4 แอทริบิวท์
บทที่ 4 แอทริบิวท์บทที่ 4 แอทริบิวท์
บทที่ 4 แอทริบิวท์Theeravaj Tum
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคTheeravaj Tum
 

More from Theeravaj Tum (10)

Javacentrix com chap10-0
Javacentrix com chap10-0Javacentrix com chap10-0
Javacentrix com chap10-0
 
Javacentrix com chap08-0
Javacentrix com chap08-0Javacentrix com chap08-0
Javacentrix com chap08-0
 
Javacentrix com chap06-0
Javacentrix com chap06-0Javacentrix com chap06-0
Javacentrix com chap06-0
 
Javacentrix com chap02-0
Javacentrix com chap02-0Javacentrix com chap02-0
Javacentrix com chap02-0
 
บทที่ 12 กราฟฟิก
บทที่ 12 กราฟฟิกบทที่ 12 กราฟฟิก
บทที่ 12 กราฟฟิก
 
บทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริบทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริ
 
บทที่ 8 คุณสมบัติก
บทที่ 8 คุณสมบัติกบทที่ 8 คุณสมบัติก
บทที่ 8 คุณสมบัติก
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์
 
บทที่ 4 แอทริบิวท์
บทที่ 4 แอทริบิวท์บทที่ 4 แอทริบิวท์
บทที่ 4 แอทริบิวท์
 
บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบค
 

บทที่ 11 การดักจับข

  • 1. การดักจับขอผิดพลาด Exception Handling วัตถุประสงค ♦ เพื่อใหผูเรียนเขาใจหลักการทํางานการดักจับขอผิดพลาดของการเขียนโปรแกรมเชิงวัตถุ ♦ เพื่อใหผูเรียนสามารถนําหลักการทํางานการดักจับขอผิดพลาดของการเขียนโปรแกรมเชิง วัตถุไปประยุกตใชเขียนโปรแกรมจริงได บทที่ 11
  • 2. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 245 เนื้อหาบทเรียน ♦ การดักจับขอผิดพลาด ♦ คําสั่ง try ♦ คําสั่ง catch ♦ คําสั่ง final ♦ คําสั่ง throw, throws ♦ การสรางคลาสตรวจจับขอผิดพลาดใชเอง
  • 3. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 246 ความผิดปกติ(Exception) Exception คือ เหตุการณผิดปกติที่เกิดขึ้น สงผลใหการทํางานของโปรแกรมไมเปนไปตาม ขั้นตอนที่ไดกําหนดไว ตัวอยางของการเกิด Exception เชน เมื่ออางถึงตัวแปรอารเรยเกินขนาดที่ กําหนด หรือการหารดวยตัวเลข 0 เปนตน โดย exception แบงออกเปนหลายประเภท ซึ่ง exception แตละประเภทจะเปนคลาสลูกของ คลาส Exception ดังรูปตอไปนี้ ตัวอยาง Exception ที่เกิดขึ้นขณะรันโปรแกรม โดยโปรแกรมีการ / ดวย 0 public class Error1 { public static void main(String[] args) { int x = 5 / 0; System.out.println("Result = " + x); } } โปรแกรมนี้เมื่อคอมไพลจะไมเกิดขอผิดพลาด ผลลัพธที่ไดจากการรันโปรแกรม java.lang.ArithmeticException: / by zero at Error1.main(Error1.java:3) Exception in thread "main"
  • 4. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 247 ตัวอยาง การเกิด Exception ขณะ array เกินขนาดที่จองไว public class Error2 { public static void main(String[] args) { int x[] = {0,1,2}; for (int i =0; i <= x.length ; i++ ) { System.out.println("Result = " + x[i]); } System.out.println(“Good bye”); } } เมื่อผานการคอมไพล ผลลัพธที่ไดจากการรันโปรแกรมคือ Result = 0 Result = 1 Result = 2 java.lang.ArrayIndexOutOfBoundsException: 3 at Error2.main(Error2.java:6) Exception in thread "main" จากการรันโปรแกรมนี้จะพบวาหากโปรแกรมไมเกิดขอผิดพลาดกอน จะพิมพขอความวา Good bye ออกบนหนาจอ แตโปรแกรมนี้ พิมพขอความ result = 0 result =1 result =2 แลวก็หยุดไป พรอมกับแสดงขอความ java.lang.ArrayIndexOutOfBoundsException: 3 แสดงวาโปรแกรมเกิด ขอผิดพลาดขณะรันโปรแกรม(runtime exception) ที่ชื่อวา ArrayIndexOutOfBoundsException ขึ้นกอน ทําใหโปรแกรมหยุดการทํางานไป การเกิด Exception ใน Java o เมื่อเกิด Exception ขึ้นในภาษา Java ตัว JVM จะสรางออปเจ็คในตระกลูของ Throwable Class ขึ้นมาแลวโยนออกมา o เมื่อ JVM โยนออปเจ็ค Exception ออกมาเราสามารถดักจับดวยคําสั่ง try, catch, finally แตถา เราไมจัดการ JVM ก็จะเปนคนจัดการเองคือแสดงการเกิด Exception และก็ลุดออกโปรแกรมดัง ตัวอยางขางตน
  • 5. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 248 Exception Hierarchy จะแบงออกเปน 3 กลุมดวยกัน ►จะแบงออกเปน 3 กลุมดวยกัน Error เปน Exception ที่เกิดขึ้นจาก JVM เองไมเกี่ยวกับการโปรแกรมของเรา เชน StackOverflowError, OutOfMemoryError RuntimeException เปน Exception ที่เกิดจากการโปรแกรมของเราซึ่งถาเกิด Exception ในตระกลูนี้ควรจะแกไขโปรแกรมโดยไมใช try – catch - finally เชน ArithmeticException, NullPointerException, IndexOutOfBoundsException Exception เปน Exception ที่จะตองดักจับเชน FileNotFoundException, EOFException วิธีการจัดการกับ Exception Exception สามารถเกิดขึ้นไดกับโปรแกรมเสมอ ทุกที่ทุกเวลา ดังนั้นเพื่อใหโปรแกรมของเรา ทํางานจบอยางปกติแมวาจะเกิดขอผิดพลาดเหลานั้น โดยเราจะตองจัดการกับขอผิดพลาด(Exception Handing) ที่อาจเกิดขึ้นในโปรแกรม โดยระบุการดักจับ exception ไวในสวนที่คาดวาจะเกิดขอผิดพลาด และทําการจัดการกับ exception ดังกลาว ดวยคําสั่ง Try ,catch ,throw หรือ finally คําสั่ง try, catch and finally ใชในการดักจับ Exception ที่เกิดขึ้นจากการเรียกใช Methods หรือ คําสั่ง เมื่อเราจับ Exception ที่เกิดขึ้นแลว Exception ก็จะหายไปโปรแกรมก็จะทํางานตอโดยขามคําสั่งในบล็อกไป Error Exception RuntimeException Object Throwable
  • 6. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 249 ขอกําหนด เราจะนํา code ที่เราคิดวาอาจจะเกิดขอผิดผลาดมาไวในบล็อก try ถาเกิดขอผิดผลาดหรือ Exception ก็จะถูกจับโดยบล็อกของ catch หลักการทํางาน เมื่อเกิดขอผิดผลาดในบล็อกของ try JVM ก็จะโยน Exceptin มาใหกับบล็อกของ catch ตัวอยาง การใชดักจับขอผิดพลาดที่อาจเกิดขึ้น class Zero{ public static void main(String args[]) { int numerator = 10; int denominator = 0; System.out.println("This test devide by zero"); try { System.out.println(numerator/denominator); } catch(ArithmeticException e) { System.out.println("Arithemetic Exception"); System.out.println("Cann't compute"); } System.out.println("This text will not be printed."); } } ผลลัพธที่ไดคือ This test devide by zero Arithemetic Exception Cann't compute This text will not be printed. รูปแบบการใชงาน try และ catch try{ [statements] } [catch( ExceptionType1 ExceptionName1) { [statements] }]
  • 7. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 250 ตัวอยาง การใชดักจับขอผิดพลาดที่อาจเกิดขึ้น class Zero{ public static void main(String args[]) { int numerator = 10; int denominator = 0; System.out.println("This test devide by zero"); try { System.out.println(numerator/denominator); } catch(Exception e) { System.out.println(e.toString()); } System.out.println("This text will not be printed."); } } ผลลัพธที่ไดคือ This test devide by zero Arithemetic Exception java.lang.ArithmeticException: / by zero This text will not be printed. ตัวอยาง การใชดักจับขอผิดพลาดที่อาจเกิดขึ้น public class Error2 { public static void main(String[] args) { int x[] = {0,1,2}; try{ for (int i =0; i <= x.length ; i++ ) System.out.println("Result = " + x[i]); } catch (Exception e){ System.out.println("GO GOOOO"); } } } ผลลัพธที่ไดคือ Result = 0 Result = 1 Result = 2 GO GOOOO
  • 8. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 251 ตัวอยาง การใชดักจับขอผิดพลาดที่ไมถูกตอง try { System.out.println(“in of try block”); } System.out.println(“out of try block”); // ขาด block try ตัวอยาง การใชดักจับขอผิดพลาดที่อาจเกิดขึ้น try { System.out.println(“in of try block”); } System.out.println(“out of try block”); // error เพราะ try ตองตามดวย catch ทันที catch ( Exception e ) { System.out.println(“in of catch block”); } ขอกําหนด - ถามี catch บล็อกหลายตัว ExceptionType จะตองไมซ้ํากันและจะตองจัดเรียงตาม Hierarchy ดวย โดย Hierarchy ที่ต่ํากวาจะอยูดานบน Hierarchy ที่สุดกวาจะอยูดานลาง หลักการทํางาน พิจารณา Exception วา ExceptionType ที่โยนมานั้น ตรงหรือใกลเคียงกับ ExceptionType ใด ที่สุดในบล็อกของ catch รูปแบบการใช Try คูกับ หลาย Catch try { <statements> } catch (<ExceptionType1> <identifier>) { <statements> } catch(<ExceptionType2> <identifier>) { <statements> }
  • 9. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 252 ตัวอยางคลาส Exception มาตราฐาน เชน IOException , NoSuchMethodException , FileNotFoundExceptionฯ ซึ่งคลาสเหลานี้ตองทําการ import java.io.IOException; หรืออื่นๆ ตามลําดับ ถาไมรูจะตองดักจับ Exception อะไรบาง.... ทําอยางไรดี การดักจับ exception ไมใชเรื่องยากก็จริง แตเรื่องที่ยากคือ การที่เราไมรูวามี exception อะไรบางที่จะตองดักจับ ตรงนี้หากเขียนโปรแกรมไปสักพัก มีความชํานาญมากพอ แลวคงจะทําให ทราบไดเองจากประสบการณวาจะตองดักจับ exception ประเภทใดบาง แต ณ ตอนนี้หากไมทราบจริงๆ วาจะตองดักจับ exception อะไรบาง วิธีที่งายที่สุด คือ ใหดักจับ exception ประเภท Exception เพราะ exception ทุกประเภทที่เกิดขึ้นในโปรแกรม ลวนเปน ออบเจ็คของคลาสที่สืบทอดคุณสมบัติมาจาก คลาส Exception ทั้งสิ้น(จะสืบทอดโดยตรงหรือไม ไมใชเรื่องสําคัญ) ดังนั้นไมวาจะเกิด exception ประเภทใดขึ้นหากระบุใหดักจับ exception ประเภท Exception แลวจะสามารถดักจับไดทั้งหมด ตัวอยาง การดักจับขอผิดพลาด class testException{ public static void main(String[] args) { int x[] = {1, 10, 22}; for(int i = 0; i <= 3; i++) try{ System.out.println(x[i] / i ); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Error!!! array out of bound"); } catch(ArithmeticException e){ System.out.println("Error!!! divided by ZERO"); } catch(Exception e){ System.out.println("Error!!! Something..."); } System.out.println("Bye Bye"); }} ผลลัพธที่ไดคือ Error!!! divided by ZERO 10 11 Error!!! array out of bound Bye Bye
  • 10. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 253 ตัวอยาง การดักจับขอผิดพลาด import javax.swing.JOptionPane; class DivByZero{ public static void main(String args[]){ String s1, s2; int i1 , i2; try{ s1 = JOptionPane.showInputDialog("Enter number 1 : "); s2 = JOptionPane.showInputDialog("Enter number 2 : "); i1 = Integer.parseInt(s1); i2 = Integer.parseInt(s2); JOptionPane.showMessageDialog(null, Integer.toString(i1 / i2)); } catch (NumberFormatException e){//1 JOptionPane.showMessageDialog(null,e.getMessage()); System.exit(0); } catch(ArithmeticException e){//2 JOptionPane.showMessageDialog(null,e.getMessage()); System.exit(0); } catch(Exception e){//3 JOptionPane.showMessageDialog(null,e.getMessage()); System.exit(0); } System.exit(0); } } จากโปรแกรมจะทําการแสดง Dialog box เพื่อรับตัวเลขจํานวน 2 ครั้ง แลวนําตัวแรกเปนตัวตั้ง แลวเอาคาที่สองเปนตัวหาร ซึ่งโปรแกรมไดทําการดักจับ error ไว 2 รูปแบบและหากเกิดขอผิดพลาด จากนั้นจะกระทําใน Exception ที่ 3 ตัวอยางที่ผิด try { RandomAccessFile raf = new RandomAccessFile(“myfile.txt”, “r”); byte b[] = new byte[1000]; raf.readFully(b, 0, 1000); } catch ( IOException e ) {
  • 11. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 254 System.out.println(“IO Error”); } catch ( FileNotFoundException e ) { System.out.println(“File not found”); } ผิดเนื่องจาก FileNotFoundException เปน sup class ของ IOException ดังนี้ ใหถูกตองเปลี่ยนเปน try { RandomAccessFile raf = new RandomAccessFile(“myfile.txt”, “r”); byte b[] = new byte[1000]; raf.readFully(b, 0, 1000); } catch ( FileNotFoundException e ) { System.out.println(“File not found”); } catch ( IOException e ) { System.out.println(“IO Error”); } การใช try-catch block อยางปกติ จะทําใหการดําเนินของโปรแกรมผานเขามาที่จุดเดียวและผาน ออกไปที่จุดเดียว แตหากใน try block หรือ catch block มีการใชประโยคควบคุมที่ทําใหการดําเนินของ โปรแกรมกระโดดออกไปจากเสนทางปกติ เชน break , continue ,หรือ return ซึ่งไมสามารถคาดเดาได ลวงหนาวาโปรแกรมจะดําเนินออกไปจาก try-catch ที่จุดใด ในลักษณะเชนนี้ถามีโปรแกรมที่ตอง ทํางานหลังจากจบ try-catch block นั้น เชน การปดไฟลหรือ ปด socket หรือการคืนหนวยความจําของ graphic หากนําโปรแกรมเหลานี้มาไวตอจาก catch block ก็จะทําใหโปรแกรมเหลานี้ไมถูกทํางานในบาง กรณี ดังนั้นภาษาจาวา จึงมี finally block ไวสําหรับใหโปรแกรมที่ตองถูกทํางานเสมอไมวาโปรแกรม จะดําเนินออกจาก try-catch block ในเสนทางใด
  • 12. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 255 ขอกําหนด - ถามีบล็อก try จําเปนตองมีบล็อก catch อยางนอย 1 บล็อก หรือถามีมีบล็อก catch ก็ตองมี บล็อก finally หลักการทํางาน - เมื่อสิ้นสุดการทํางานบล็อกของ try…catch แลว ไมวาจะเกิด Exception หรือไมก็จะตองเขามา ทํางานในบล็อกของ finally ทุกครั้ง - ถึงแมจะสั่งคําสั่ง return หรือ System.exit() ใน try บล็อก หรือ catch บล็อก ก็จะตองเขามา ทํางานใน Finally บล็อกทุกครั้ง ตัวอยาง การเขียนโปรแกรมที่มีการดักจับ โดยใช Try – catch – finally public class Error4 { public static void main(String[] args) { try{int x = 5 / 0; System.out.println("Result = " + x); } catch(ArithmeticException a){ System.out.println("Arithmeitc Error"); } finally{ System.out.println("Finally Print"); } }} รูปแบบการทํางาน Try , catch , finally try { …….. } catch (IOException e) { …….. } catch (Exception e) { …….. } finally { //statements placed here will always //get executed } //// ffiinnaallllyy จะทํางานทุกครั้งไมวาจะเกิดจะทํางานทุกครั้งไมวาจะเกิด eexxcceeppttiioonn ขึ้นหรือไมก็ตามขึ้นหรือไมก็ตาม
  • 13. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 256 ผลลัพธที่ไดคือ Arithmeitc Error Finally Print ตัวอยาง การเขียนโปรแกรมที่มีการดักจับ โดยใช Try – finally public class Error4 { public static void main(String[] args) { try{ int x = 5 / 0; System.out.println("Result = " + x); } finally{ System.out.println("Finally"); } } } ผลลัพธที่ไดคือ Finally java.lang.ArithmeticException: / by zero at Error3.main(Error3.java:3) Exception in thread "main" ตัวอยาง โปรแกรมที่ทําการดักจับขอผิดพลาดโดยใช try catch finally String s = null; try { int x = s.length(); System.out.println( x ); } catch ( Exception e ) { // Exception e = Exception System.out.println(“Catch”); } finally { System.out.println(“Finally”); } ผลลัพธคือ Catch Finally
  • 14. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 257 ตัวอยาง ที่ผิดไมควรเขียนบล็อก finally กอน catch String s = null; try { int x = s.length(); System.out.println( x ); } finally { System.out.println(“Finally”); } catch ( Exception e ) { System.out.println(“Catch”); } ผลลัพธที่ไดคือ : Compiled Error ไมสามารถเขียนบล็อก finally ไวกอนบล็อก catch ตัวอยาง การตรวจสอบ Null String s = null; try { int x = s.length(); System.out.println( x ); } finally { System.out.println(“Finally”); } ผลลัพธคือ Finally Exceptoin in thread “method” java.lang.NullPointException at ClassName.method(FileName.java:15) ตัวอยาง การตรวจจับขอผิดพลาด ดวย try catch finally class testException3{ public static void main(String[] args) { int x[] = {1, 10, 22}; try{ for(int i = 0; i <= 3; i++) System.out.println(x[i] / i ); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Error!!! array out of bound"); }
  • 15. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 258 catch(ArithmeticException e){ System.out.println("Error!!! divided by ZERO"); } catch(Exception e){ System.out.println("Error!!! Something..."); } finally{ System.out.println("finally print"); } System.out.println("Bye Bye"); } } ผลลัพธที่ไดคือ Error!!! divided by ZERO finally print Bye Bye ตัวอยาง การตรวจจับขอผิดพลาด ดวย try catch finally class testException2{ public static void main(String[] args) { int x[] = {1, 10, 22}; for(int i = 0; i <= 3; i++) try{ System.out.println(x[i] / i ); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Error!!! array out of bound"); } catch(ArithmeticException e){ System.out.println("Error!!! divided by ZERO"); } catch(Exception e){ System.out.println("Error!!! Something..."); } finally{ System.out.println("finally print"); } System.out.println("Bye Bye"); }}
  • 16. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 259 ผลลัพธที่ไดคือ Error!!! divided by ZERO finally print 10 finally print 11 finally print Error!!! array out of bound finally print Bye Bye การจัดการ Exception ที่เกิดใน Method สามารถทําได 2 แบบ o ใช try…catch…finally ดักจับ Exception ใน Methods เลย(แบบฝกหัดที่ 2) o โยน Exception ออกใหกับผูเรียก(Method)ใช โดยใช throw กับ throws Throws Exception หากใน method ใดมีประโยคที่อาจจะมีการสง exception ออกมา แตไมมีการดักจับและจัดการ กับ exception นั้น เราจะตองระบุให method นั้นเปน method ที่จะสง exception ออกมาโดยใช keyword ‘throws’ กํากับไวหลังจากวงเล็บของพารามิเตอร และตามดวยคลาสของ exception ที่จะถูกสงออกมา การเรียกใช method ที่ถูกระบุวาจะสง exception ออกมาจะตองมีการดักจับและจัดการกับ exception นั้น มิเชนนั้นจะคอมไพลไมผาน หากมี exception เกิดขึ้นในเมธอดหนึ่ง แตไมถูกดักจับภายในเมธอดนั้น จะทําให exception ถูก สงตอ(propagate) ใหกับเมธอดที่เรียกเมธอดนั้นใหทํางาน เชน method main เรียก method A ใหทํางาน และเมธอด A เรียก method B ใหทํางาน หากเกิด exception ขึ้นใน method B แต method B ไมไดดักจับ exception นั้น โปรแกรมจะสง exception ไปให method A ตอไป และถา method A ไมดักจับ exception นั้นอีก method main ซึ่งเปนผูเรียก method A ใหทํางานก็จะตองดักจับและจัดการกับ exception นั้น ซึ่งเมธอด main จะหลีกเลี่ยงการดักจับ exception ไมไดแลว เพราะหากยังไมมีการดักจับ exception ที่ method main อีก จะสงผลใหโปรแกรมจบการทํางานลงแบบไมสมบูรณ สรุป - catch คือ การดักจับ exception วาเปนของเหตุการณใดและจัดการกับเหตุการณนั้น - throw คือ หากเกิดขอผิดพลาดบางอยาง exception ถูกโยนออกจากโปรแกรม
  • 17. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 260 รูปแบบการใช Throws กรณีที่ 1 : การเกิด exception(การโยน exception)จาก method [Modifier] return_type method_name([argumentsล) throws ExceptionType [, ExceptionType,…] { [statements] throw exception_object [statements] } กรณีที่ 2 : หาก exception ไมไดรับการดักจับภายใน method ที่โยน exception นั้นออกมา method ที่เปน ผูเรียกเมธอดนั้นใหทํางานจะตองเลือกระหวางการดักจับ exception หรือ สงตอ exception ใหกับเมธอด ที่เรียกใชงาน อยางใดอยางหนึ่ง ซึ่งในการสงตอ exception จะตองประกาศ method ดังนี้ [Modifier] return_type method_name([argumentsล) throws ExceptionType [, ExceptionType,…] { [statements] } เปนการประกาศวา method นั้นอาจจะโยนออปเจ็ค Exception ประเภทนั้นๆ(ExceptionType) ออกไป และสามารถมี ExceptionType ไดหลายตัวแตไมซ้ํากัน ตัวอยาง การประกาศ Throws void method1() throws NullPointerException, IOException { . . . } คําสั่ง Throws รูปแบบ throw ExceptionObject; เปนการสั่งให JVM โยนออปเจ็ค Exception ที่เราระบุออกไปโดยไมไดเกิดขอผิดผลาดใดๆ
  • 18. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 261 ตัวอยาง การใชคําสั่ง throw if ( x == 0 ) { throw new ArithmeticException(); } else { return 5 / x; } ตัวอยาง การใช throw และ throws รวมกัน - ตัวอยางนี้เกิด Compiled Error void method1() { throw new FileNotFoundException(); } - ที่ถูกตองเปน void method1() throws FileNotFoundException { throw new FileNotFoundException(); } - หรือที่ถูกตองเปน void method1() throws IOException { throw new FileNotFoundException(); } ตัวอยาง การจับ Exception ที่เราสรางขึ้น - ตัวอยางที่ผิด void method1() throws IOException { throw new FileNotFoundException(); } void method2() { try { method1(); } catch (FileNotFoundException e) { } }
  • 19. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 262 - ที่ถูกตองเปน void method2() { try { method1(); } catch (IOException e) { } } ผลของการประกาศ throws - เมื่อนํา Method นั้นไปใชจะตองใชคําสั่ง try…catch คอยดักจับ Exception ในชนิดนั้นๆ - เราสามารถใชชนิดของ Exception ที่มี Hierarchy ที่สูงกวามารับ ตัวอยางดักจับ Method ที่ประกาศ throws void method1() throws IOException { throw new FileNotFoundException(); } void method2() { try { method1(); } catch (IOException e) { System.out.println(“IOException”); } } ผลลัพธที่ไดคือ IOException ตัวอยางดักจับ Method ที่ประกาศ throws void method1() throws IOException { throw new FileNotFoundException(); }
  • 20. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 263 void method2() { try { method1(); } catch (FileNotFoundException e) { Sytem.out.println(“FileNotFoundException”); } catch (IOException e) { System.out.println(“IOException e”); } } ผลลัพธที่ไดคือ FileNotFoundException ตัวอยาง throw และ throws เมื่อ method 2 เรียกใช method 1 แลว method 1 ได throws exception ทํา ให method ที่เรียกใชงานตอง thows exception เดียวกัน หรือ ซุปเปอรคลาสของ exception นั้น - การประกาศ method ที่มีการ throws แลวเกิด ขอผิดพลาด compile error void method1() throws IOException { throw new FileNotFoundException(); } void method2() { method1(); } - ที่ถูกตองเปน void method1() throws IOException { throw new FileNotFoundException(); } void method2() throws IOException { method1(); }
  • 21. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 264 ตัวอยาง การ throws ออกจาก method - ทําการคอมไพลแลวเกิด compile error void method1() { throw new FileNotFoundException(); } - ที่ถูกตองเปน void method1() throws FileNotFoundException { throw new FileNotFoundException(); } - หรือ ที่ถูกตองเปน void method1() throws IOException { throw new FileNotFoundException(); } ตัวอยาง การThrows ออกจาก method class testThrows{ static int div(int x , int y) throws ArithmeticException { return (x/y); } public static void main(String args[]){ try { System.out.println(div(15,0)); } catch (ArithmeticException e){ System.out.println(e.toString()); System.out.println(e.getMessage()); } }} ผลลัพธที่ไดคือ java.lang.ArithmeticException: / by zero / by zero
  • 22. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 265 ตัวอยาง การ throw exception ออกจาก method class testThrow1{ static int div(int x, int y){ try{ if(y==0) throw new ArithmeticException(); return x/y; } catch (ArithmeticException e){ return Integer.MAX_VALUE; // 2147483647 } } public static void main(String[] args) { System.out.println(div(1,0)); } } ผลลัพธที่ไดคือ 2147483647 ตัวอยาง class ThrowException{ void method1(double a, double b){ try{ System.out.println(method2(a,b)); } catch(ArithmeticException ex){ System.out.println(ex.getMessage()); } } String method2(double a, double b) throws ArithmeticException{ if (b==0){ System.out.println(a + "/" + b + "==>"); throw new ArithmeticException(); } else return a + "/" + b + "=" + a / b; } public static void main(String args[]){ ThrowException t = new ThrowException();
  • 23. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 266 t.method1(20.0,3.0); t.method1(20.0,0.0); t.method1(20.0,5.0); } } ผลลัพธคือ 20.0/3.0=6.666666666666667 20.0/0.0==> null 20.0/5.0=4.0 ตัวอยาง throw และ throws public class Test { public static void aMethod() throws Exception { try { throw new Exception(); } finally { System.out.println(“finally”); } } public static void main(String args[]) { try { aMethod(); } catch (Exception e) { System.out.println(“exception”); } System.out.println(“finished”); } } ผลลัพธที่ไดคือ finally exception finished
  • 24. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 267 Exception Classes ของเราเอง เราสามารถที่จะสืบทอด Exception Class ใชเปนชนิดของเราเองได Excection Class ที่เราจะสืบทอดเพื่อนํามาใชควรเปนในตระกลู Exception ไมควรใช Error หรือ RuntimeException ตัวอยาง การสรางคลาส Exception ใชงานเอง class MyException extends Exception { } public class Test { public static void aMethod() throws MyException { throw new MyException(); } public static void main(String args[]) { try { aMethod(); } catch (MyException e) { System.out.println(“MyException”); } catch (Exception e) { System.out.println(“Exception”); } } } ผลลัพธที่ไดคือ MyException ตัวอยาง การสรางคลาส Exception ใชงานเอง class MyException extends Exception{ MyException(String s){ super(s); } } public class ExcepScope{ static void sayHello(String s) throws MyException{ if (s.equals("John")) throw new MyException("Bad Guy"); System.out.println("Hello !" + s); }
  • 25. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 268 public static void main(String args[]){ String n[] = {"Joe" , "Jack" , "John" ,"Jim"}; for(int i = 0 ; i < n.length ; i++){ try{ sayHello(n[i]); } catch (MyException e){ System.out.println("No hello for a bad guy"); } } }} ผลลัพธที่ไดคือ Hello !Joe Hello !Jack No hello for a bad guy Hello !Jim
  • 26. บทที่ 11 การดักจับขอผิดพลาด หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 269 แบบฝกหัด 1. ใหนิสิตทําการดักจับขอผิดพลาดที่เกิดในโปรแกรมนี้ public class Error1 { public static void main(String[] args) { int x = 5 / 0; System.out.println("Result = " + x); } } สิ่งที่ไดขณะ run โปรแกรมคือ Exception in thread "main" java.lang.ArithmeticException: / by zero at Error1.main(Error1.java:3) 2. จากโปรแกรมตอไปนี้ class testFinally1{ public static void main(String args[]){ String input ; input = "Madee" ; // **TEST*** try{ if ((input.equals("Madee"))) return; System.out.println("Hello " + input); } catch (Exception e){ System.out.println("Hello , whoever you are"); } finally { System.out.println("How are you today?"); } System.out.println("It's nice to see you"); } } จากโปแกรม testFinally1 ทําการทดลองเปลี่ยนแปลงคาในบรรทัด //** TEST *** 3 กรณีดังตอไปนี้ 1 . input = null; 2. input = “Joe“ 3. input = "Madee" ; ผลลัพธที่ไดทั้งสามกรณีคืออะไร พรอมทั้งอธิบายเหตุผล