High Fashioners Issue # 2 | Page 218

J. E. N. I.
Sekarang anda sudah cukup familiar dengan beberapa exception classes, saatnya untuk mengenalkan aturan: catch lebih dari satu harus berurutan dari subclass ke superclass. class MultipleCatchError { public static void main( String args []){ try { int a = Integer. parseInt( args [ 0 ]); int b = Integer. parseInt( args [ 1 ]); System. out. println( a / b);
} catch( Exception e) { System. out. println( e);
} catch( ArrayIndexOutOfBoundsException e2) {
System. out. println( e2);
} System. out. println(" After try-catch-catch.");
}
}
Setelah mengkompilasi kode tersebut akan menghasilkan pesan error jika Exception class adalah superclass dari ArrayIndexOutOfBoundsException class.
MultipleCatchError. java: 9: exception java. lang. ArrayIndexOutOfBoundsException has already been caught } catch( ArrayIndexOutOfBoundsException e2) {
2.5.2 Checked dan Unchecked Exceptions
Exception terdiri atas checked dan unchecked exceptions.
Checked exceptions adalah exception yang diperiksa oleh Java compiler. Compiler memeriksa keseluruhan program apakah menangkap atau mendaftar exception yang terjadi dalam sintax throws. Apabila checked exception tidak didaftar ataupun ditangkap, maka compiler error akan ditampilkan.
Tidak seperti checked exceptions, unchecked exceptions tidak berupa compile-time checking dalam penanganan exceptions. Fondasi dasar dari unchecked exception classes adalah Error, RuntimeException dan subclass-nya.
2.5.3 User Defined Exceptions
Meskipun beberapa exception classes terdapat pada package java. lang namun tidak mencukupi untuk menampung seluruh kemungkinan tipe exception yang mungkin terjadi. Sehingga sangat mungkin bahwa anda perlu untuk membuat tipe exception tersendiri. Dalam pembuatan tipe exception anda sendiri, anda hanya perlu untuk membuat sebuah extended class terhadap RuntimeException class, maupun Exception class lain. Selanjutnya tergantung pada anda dalam memodifikasi class sesuai
Pengenalan Pemrograman 2 9