- try digunakan untuk mendefinisikan pernyataan yang memungkinkan timbulnya exception.
- catch digunakan untuk menangani exception ketika muncul.
- finally digunakan untuk menutup proses sebelumnya, dimana pernyataan ini boleh untuk tidak digunakan.
public class TestTryCatch{ public static void main(String[] args){ int i = 1; int j = 0; try{ System.out.println("Try block entered " + "i = "+ i + " j = "+j); System.out.println(i/j); System.out.println("blok try berakhir"); }catch(ArithmeticException e) { System.out.println("terjadi exception"); } System.out.println("setelah blok try"); return; } }
Perhatikan juga contoh berikut:
import java.io.IOException; public class TryBlockTest{ public static void main(String[] args) throws IOException{ int[] x = {10, 5, 0}; try{ System.out.println("Blok try pertama"); System.out.println("hasil = " + divide(x,0)); x[1] = 0; System.out.println("hasil = " + divide(x,0)); x[1] = 1; System.out.println("hasil = " + divide(x,1)); } catch(ArithmeticException e){ System.out.println("Arithmetic exception”); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Index-out-of-bounds exception"); } System.out.println("\nTekan Enter untuk keluar"); try { System.out.println("blok try kedua"); System.in.read(); return; }catch(IOException e){ System.out.println("I/O exception "); }finally{ System.out.println("blok finally"); } } public static int divide(int[] array, int index){ try{ System.out.println("\nblok try pertama"); array[index + 2] = array[index]/array[index + 1]; return array[index + 2]; }catch(ArithmeticException e) { System.out.println("Arithmetic exception"); }catch(ArrayIndexOutOfBoundsException e) { System.out.println("Index-out-of-bounds exception"); }finally{ System.out.println("blok finally"); } return array[index + 2]; } }
Tidak ada komentar:
Posting Komentar