โจทย์ Java Exception

ทดสอบความเข้าใจเกี่ยวกับ exception

http://bit.ly/javaBook

http://wp.me/prjdf-11d

public class Final {
  public static void main(String[] args) {
    String[] a = {"10", "10.5", "abc"};
    int i = Integer.parseInt(a[0]);
    i++;
    System.out.println(i);
  }
}

ตอบ ………………………………….

public class Final {
  public static void main(String[] args) {
    try {
      int i = 0;
      System.out.println(10 / i);
    } catch (Exception ex) {
      System.out.println("CAT");
    } finally {
      System.out.println("FIN");
    }
  }
}

ตอบ ………………………………….

public class Final {
 public static void main(String[] args) {
    String[] a = {"10", "10.5", "abc"};
    for (int i = 0; i < 4; i++) {
      try {
        int n = Integer.parseInt(a[i]);
        n++;
        System.out.println(n);
      } catch (Exception ex) {
        System.out.println(ex);
      }
    }
  }
}

ตอบ ………………………………….

public class Final {
 public static void main(String[] args) {
    String[] a = {"10", "10.5", "abc"};
    for (int i = 0; i < 4; i++) {
      try {
        int n = Integer.parseInt(a[i]);
        n++;
        System.out.println(n);
      } catch (NumberFormatException ex) {
        System.out.println("NUM");
      } catch (ArrayIndexOutOfBoundsException ex) {
        System.out.println("INDEX");
      } catch (Exception ex) {
        System.out.println("EX");
      }
    }
  }
}

ตอบ ………………………………….

public class Final {
 public static void main(String[] args) {
    String[] a = {"10", "10.5", "abc"};
    for (int i = 0; i < 4; i++) {
      try {
        int n = Integer.parseInt(a[i]);
        n++;
        System.out.println(n);
      } catch (Exception ex) {
        System.out.println("EX");
      } catch (NumberFormatException ex) {
        System.out.println("NUM");
      } catch (ArrayIndexOutOfBoundsException ex) {
        System.out.println("INDEX");
      }
    }
  }
}

ตอบ ………………………………….

public class Final {
  public static void foo() throws Exception {
  }

  public static void main(String[] args) {
    foo();
  }
}

คอมไพล์ผ่านไหม …..

public class Final {
  public static void foo() throws RuntimeException {
  }

  public static void main(String[] args) {
    foo();
  }
}

คอมไพล์ผ่านไหม …..

public class Final {
  public static void foo() throws NullPointerException {
  }

  public static void main(String[] args) {
    foo();
  }
}

คอมไพล์ผ่านไหม …..

public class Final {
  public static void foo(int n) throws Exception {
    if (n == 2)
      throw new Exception();
    System.out.println("F" + n);
  }

  public static void main(String[] args) {
    try {
      foo(1);
      foo(2);
      foo(3);
    } catch (Exception ex) {
      System.out.println("CAT");
    }
  }
}

ตอบ ………………………………….

public class Final {
  public static void foo(int n) throws Exception {
    if (n == 2) {
      throw new RuntimeException();
    }
    System.out.println("F" + n);
  }

  public static void main(String[] args) {
    foo(2);
  }
}

คอมไพล์ผ่านไหม …..

import java.io.*;

public class Final {
  public static void foo(int n) throws FileNotFoundException {
  }

  public static void main(String[] args) throws Exception {
    foo(5);
  }
}

คอมไพล์ผ่านไหม …..

import java.io.*;

public class Final {
  public static void foo(int n) throws Exception {
  }

  public static void main(String[] args) throws FileNotFoundException {
    foo(5);
  }
}

คอมไพล์ผ่านไหม …..

import java.io.FileReader;

public class NewClass {
  public static void main(String[] args) {
    FileReader fr = new FileReader("data.txt");
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileReader;

public class NewClass {
  public static void main(String[] args) throws Exception {
    FileReader fr = new FileReader("data.txt");
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("data.txt");
    } catch (Exception ex) {
      System.out.println("CAT");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileNotFoundException;
import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("data.txt");
    } catch (FileNotFoundException ex) {
      System.out.println("CAT");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileNotFoundException;
import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("data.txt");
    } catch (Exception ex) {
      System.out.println("CAT1");
    } catch (FileNotFoundException ex) {
      System.out.println("CAT2");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileNotFoundException;
import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("data.txt");
    } catch (FileNotFoundException ex) {
      System.out.println("CAT1");
    } catch (Exception ex) {
      System.out.println("CAT2");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..


import java.io.FileNotFoundException;
import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("data.txt");
    } catch (NumberFormatException ex) {
      System.out.println("CAT1");
    } catch (FileNotFoundException ex) {
      System.out.println("CAT2");
    } catch (Exception ex) {
      System.out.println("CAT3");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..


import java.io.FileNotFoundException;
import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("data.txt");
    } catch (FileNotFoundException ex) {
      System.out.println("CAT2");
    } catch (NumberFormatException ex) {
      System.out.println("CAT1");
    } catch (Exception ex) {
      System.out.println("CAT3");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileNotFoundException;
import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("data.txt");
    } catch (Exception ex) {
      System.out.println("CAT");
    } finally {
      System.out.println("FIN");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    FileReader fr = null;
    try {
      fr = new FileReader("data.txt");
    } catch (Exception ex) {
      System.out.println("CAT");
    } finally {
      if (fr != null) fr.close();
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) throws Exception {
    FileReader fr = null;
    try {
      fr = new FileReader("data.txt");
    } catch (Exception ex) {
      System.out.println("CAT");
    } finally {
      if (fr != null) fr.close();
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try (FileReader fr = new FileReader("data.txt")) {
      System.out.println("TRY");
    } catch (Exception ex) {
      System.out.println("CAT");
    } 
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try (FileReader fr0 = new FileReader("data0.txt");
         FileReader fr1 = new FileReader("data1.txt")) {
      System.out.println("TRY");
    } catch (Exception ex) {
      System.out.println("CAT");
    } 
  }
}

คอมไพล์ผ่านหรือไม่ …..

public class NewClass {

  public static void main(String[] args) {
    try {
      int i = Integer.parseInt("10");
    } finally {
      System.out.println("FIN");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

public class NewClass {

  public static void main(String[] args) {
    try {
      int i = Integer.parseInt("10.5");
    } finally {
      System.out.println("FIN");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

import java.io.FileReader;

public class NewClass {

  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader("data.txt");
    } finally {
      System.out.println("FIN");
    }
  }
}

คอมไพล์ผ่านหรือไม่ …..

public class NewClass {
  public static void main(String[] args) {
    throw new RuntimeException();
  }
}

คอมไพล์ผ่านหรือไม่ …..

public class NewClass {
  public static void main(String[] args) {
    throw new RuntimeException();
    int i = 10;
  }
}

คอมไพล์ผ่านหรือไม่ …..

public class NewClass {
  public static void main(String[] args) {
    throw new Exception();
  }
}

คอมไพล์ผ่านหรือไม่ …..

public class NewClass {
  public static void main(String[] args) throws Exception {
    throw new Exception();
  }
}

คอมไพล์ผ่านหรือไม่ …..

interface A {

  void go() throws Exception;
}

public class B implements A {
  public void go() {
    
  }
}

คอมไพล์ผ่านหรือไม่ …..


import java.io.IOException;


interface A {

  void go() throws IOException;
}

public class B implements A {
  public void go() throws Exception {
    
  }
}

คอมไพล์ผ่านหรือไม่ …..


import java.io.FileNotFoundException;
import java.io.IOException;


interface A {

  void go() throws IOException;
}

public class B implements A {
  public void go() throws FileNotFoundException {
    
  }
}

คอมไพล์ผ่านหรือไม่ …..


11

CAT
FIN

11
java.lang.NumberFormatException: For input string: “10.5”
java.lang.NumberFormatException: For input string: “abc”
java.lang.ArrayIndexOutOfBoundsException: 3

11
NUM
NUM
INDEX

X

X

O

O

F1
CAT

X

O

X

X

O

O

O

X

O

O

O

O

X

O

O

O

O

O
(exception thrown, FIN)

X

O

X

X

O

O

X

O


หนังสือ เขียนโปรแกรม Java เบื้องต้น

หากจะนำข้อความไปใช้ ต้องแสดงที่มา และห้ามใช้ในเชิงพาณิชย์

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s