วันพุธที่ 8 มีนาคม พ.ศ. 2560

Java language [ Try Catch Finally ]

ภาษาจาวามีคีย์เวิร์ด finally ที่จะมีชุดคำสั่งอยู่ในบล็อกเพื่อระบุให้โปรแกรมทำชุดคำสั่งดังกล่าวหลังจากสิ้นสุดการทำงานของชุดคำสั่งในบล็อก tryหรือ catch

ภาษาจาวาจะทำชุดคำสั่งในบล็อก finally เสมอ แม้ว่าจะมีคำสั่งreturn ในบล็อก try หรือ catch ก่อนก็ตาม กรณีเดียวที่จะไม่ทำชุดคำสั่งในบล็อก finally คือมีคำสั่ง System.exit();

  ตัวอย่างการใช้ try and catch
public class ExceptionHandlingDemoV2 {
public static void main(String args[]) {
try {
int i = Integer.parseInt(args[0]);
System.out.println(4 / i);
} catch(ArithmeticException ex) {
System.out.println(ex.toString());
} catch(NumberFormatException ex) {
System.out.println("Invalid numeric format");
}
}
}
4เราสามารถจะจัดการกับออปเจ็คของ Exception โดยใช้คลาสที่เป็น superclass ของ  Exception นั้นได้ อาทิเช่น เราสามารจัดการกับFileNotFoundException โดยใช้ IOExceptionหรือ Exception แทนได้
4การจัดการกับ Exception มีสองแบบ คือ
ใช้คำสั่ง try/catch
ใช้คำสั่ง throws ในการประกาศเมธอดที่จะมีการเรียกใช้เมธอดใดๆที่อาจส่งออปเจ็คประเภท Exception

4รูปแบบการใช้คำสั่ง throws มีดังนี้
[modifier] return_type methodName([arguments]) throws
ExceptionType[,ExceptionType2] {
...
}
4ตัวอย่าง
public void openfile(String s) throwsFileNotFoundException {
...
   }

4เมธอดใดๆสามารถที่จะจัดการกับ Exception โดยใช้คำสั่งthrows ได้มากกว่าหนึ่งประเภท
4ตัวอย่าง
public void openFile(String s)throwsFileNotFoundException,UnknownHostException{
 ...
      }
4กรณีที่มีการใช้คำสั่ง throws ส่งต่อไปเรื่อยๆ แล้วเมธอด main()ซึ่งเรียกใช้เมธอดสุดท้ายที่ใช้คำสั่ง throws ไม่มีการจัดการกับออปเจ็คประเภท Exception ดังกล่าว  โปรแกรมจะเกิดข้อผิดพลาดในขั้นตอนการรันโปรแกรม เมื่อมีข้อผิดพลาดของออปเจ็คประเภท Exception ดังกล่าวเกิดขึ้น

4เมธอดแบบ overriden จะไม่อนุญาตให้มีการจัดการออปเจ็คประเภท Exception โดยใช้คำสั่ง throws มากชนิดกว่าที่เมธอดเดิมจัดการอยู่ได้

import java.io.*;
public class Parent {
  public void myMethods() throws FileNotFoundException { }
}
public class OverrideExceptionV2 extends Parent {
public void myMethods() throwsFileNotFoundException,IOException {
new FileInputStream("temp.txt");
}
}

4การสร้างคลาสประเภท Exception ขึ้นมาใหม่ สามารถทำได้โดยนิยามคลาสใดๆให้สืบทอดมาจากคลาสที่ชื่อ Exception
4โดยทั่วไปคลาสที่ชื่อ Exception จะมี constructor สองรูปแบบคือ
public Exception()
public Exception(String s)
4ดังนั้นคลาสที่สืบทอดมาจากคลาสที่ชื่อ Exception ควรจะมี constructor ทั้งสองแบบ โดยรูปแบบหนึ่งจะมี argument ที่มีชนิดข้อมูลเป็นString และมีคำสั่งแรกใน constructor เป็นคำสั่ง super(s);

public class MyOwnException extends Exception {
public MyOwnException (String s) {
super(s);
}
}

4เมธอดที่ต้องการส่งออปเจ็คประเภท Exception  เมื่อเกิดข้อผิดพลาดขึ้นในคำสั่งใด จะต้องเรียกใช้คำสั่งที่ชื่อ  throw  เพื่อจะสร้างออปเจ็คของคลาสประเภท Exception ขึ้นมา
4รูปแบบ
throw new ExceptionType([arguments])
4นอกจากนี้คำสั่งประกาศเมธอดนั้นจะต้องมีคำสั่ง throws เพื่อกำหนดให้คำสั่งในเมธอดอื่นๆที่เรียกใช้เมธอดนี้ต้องเขียนคำสั่งในการจัดการกับข้อผิดพลาดนี้

import java.io.*;
public class FileHandler {
    public static void openFile(String filename) throws      MyOwnException {
 File f = new File(filename);
 if (!f.exists()) {
throw new MyOwnException("File Not Found");
 }
    }
}

public class FileOpener {
 public static void main (String args[]) {
try {
FileHandler.openFile(args[0]);
System.out.println("Open successful");
} catch (MyOwnException ex) {
System.err.println(ex);
}
 }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

Set Enable .Net framework 3.5 on windows 10 (ติดตั้ง .Netframework 3.5 บนวินโดวน์ 10)

1. Download windows 8.1 file [ TH : ดาวน์โหลดไฟล์ ของ windows 8.1 ] 2. Mount File windows Copy file \sources\sxs from mount drive ...