Download M9-Exception Handling

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Exception Handling
Object Oriented Programming with JAVA
2012/2013
Error Case
Pembagian bilangan dengan 0
 Pengisian elemen array diluar ukuran array
 Operand yg akan dimanipulasi out of
prescribed range
 Mengakses obyek yang belum diinisialisasi
 Kegagalan koneksi database
 File yang akan dibuka tidak exist

Apa yg terjadi jika terjadi kesalahan?
Secara otomatis akan dilempar sebuah
object yang disebut dgn exception.
„
Exception : Event yang terjadi ketika ada
kesalahan/error saat eksekusi program.

Example 1.
Example 1. (result)
Exception Handling
Exception handling : Suatu mekanisme
penanganan exception.
 Exception dapat diproses lebih lanjut oleh
fungsi-fungsi yang siap menangani
kesalahan.

Kenapa Exception Handling ?
•
Menghasilkan program yang :
– handal : seluruh fungsionalitasnya dapat berjalan
dengan baik
– fault tolerance : fleksibel terhadap kesalahan yang
terjadi saat eksekusi
Hirarki Exception
Method pada Class Exception
try......catch......
„ lok try : digunakan untuk menempatkan
B
kode-kode program java yang mungkin
menghasilkan exception.
„
Blok catch : digunakan untuk
menempatkan kode-kode program java
yang akan menangani sebuah exception
tertentu.

Rumus Umum
try {
statement yang menyebabkan exception
} catch (namaKelasException exp1) {
statement penanganan exception
}
Blok catch dapat ditambahkan sebanyak
mungkin (lebih dari satu) disesuaikan dengan
kemungkinan jenis exception yang dapat terjadi.
Penanganan Exception
Multiple block catch

Blok catch untuk menangkap exception boleh lebih dari
satu dengan class exception yang berbeda. Contoh sintak
:
try {
Kode_program1;
catch (classException exp1){
}
catch (classException exp2){
}
….
catch (classException expN){
}
}
Alur Program
Rumus Umum dengan Finally
try {
statement yang menyebabkan exception
} catch (namaKelasException exp1) {
statement penanganan exception
} finally {
statement finally
}
Keyword : throw & throws
•
throw
Kata throw digunakan untuk secara eksplisit
melemparkan exception dalam program.
•
throws
Kata throws dituliskan di header method
(deklarasi method) apabila implementasi dari
method berpotensi menghasilkan exception.
Class SmallInt
class SmallInt{
int value;
SmallInt(int val){
value = val;
}
void plus(SmallInt X) throws SmallIntExcept{
value = value + X.value;
if (value > 10)
throw new SmallIntExcept (“TOO BIG”);
if (value < 0)
throw new SmallIntExcept (“TOO SMALL”);
}
public String toString() {
return Integer.toString(value);
}
void ReadVal () {
Scanner s = new Scanner(Sytem.in);
value = s.nextInt();
}
Main Program
class SmallIntExample {
public static void main (String args[]) {
System.out.println("start of smallint ...");
SmallInt S1= new SmallInt(1);
SmallInt S = new SmallInt();
S.ReadVal ();
try {
S1.plus (S);
System.out.println("hasil S1= S1+S ="+S1);
}catch (SmallIntExcept e) {
e.response ();
}
}
}
Alur Eksekusi Exception
Membuat Kelas Exception
Programmer dapat membuat sendiri kelas-kelas
exception
• Caranya : menurunkan dari kelas exception yang
sudah ada (gunakan extends).
• Usahakan diturunkan dari kelas exception yang
paling berdekatan.
• Agar seragam, nama kelas exception tetap
berakhiran exception.
•
Membuat Class Exceptions

Sebaiknya turunkan dari class
Exception karena sudah memiliki
method untuk mencatat pesan
exception
Contoh 2 : dengan throws
Contoh: SmallIntExcept
class SmallIntExcept extends Exception
{
private static int num_except;
SmallIntExcept(String msg){
super(msg);
num_except++;
}
static int numException (){
return num_except;
}
void response (){
System.out.println(getMessage());
}
};