10. สิ่งผิดปกติ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 10-14  ตัวอย่างการทำงานของ try-catch-finally
import java.io.*;
import java.net.*;
public class TestFinally {
  public static void main(String[] args) throws IOException {
    doSomething(1);  doSomething(2);  doSomething(3);
  }
  private static void doSomething(int k) throws IOException {
    try {
      doNothing(k);
      System.out.println("Try is sucessfully executed");
    } catch (MalformedURLException e) {
      System.out.println("catch MalformedURLException");
    } catch (FileNotFoundException e) {
      System.out.println("catch FileNotFoundException");
    } finally {
      System.out.println("Finally, I am here");
    }
  }
  private static void doNothing(int k) throws IOException {
    if (k == 2) throw new FileNotFoundException();
    if (k == 3) throw new IOException();
  }     
©2009 S.Prasitjutrakul