Professional Documents
Culture Documents
Berorientasi Objek 1
EXCEPTION
HANDLING
Pemrograman
Berorientasi Objek 1
A Little Demo
Pemrograman
Berorientasi Objek 1
A Little Demo
Exception
Pemrograman
Berorientasi Objek 1
Some Predefined
Exceptions
Java Exception
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
ClassCastException
StringIndexOutOfBoundsException
Pemrograman
Berorientasi Objek 1
Code to Cause It
String s = null;
s.length();
int a =
int b =
int q =
int[] a
a[10];
3;
0;
a/b;
= new int[10];
Object x =
new Integer(1);
String s = (String) x;
String s = "Hello";
s.charAt(5);
Pemrograman
Berorientasi Objek 1
Jenis-Jenis Exception
Exception
...
RuntimeException
...
...
Pemrograman
Berorientasi Objek 1
Jenis-Jenis Exception
Pemrograman
Berorientasi Objek 1
Jenis-Jenis Exception
Pemrograman
Berorientasi Objek 1
Exception
Deskripsi
ClassNotFoundException
CloneNotSupportedException
IllegalAccessException
InstantiationException
InterruptedException
NoSuchFieldException
NoSuchMethodException
Pemrograman
Berorientasi Objek 1
Exception
Deskripsi
AritmaticException
ArrayIndexOutBoundsException
ArrayStoreException
ClassCastException
IllegalArgumentException
Argument illegal
IllegalMonitorStateException
IllegalStateException
IllegalThreadStateException
IndexOutOfBoundException
Pemrograman
Berorientasi Objek 1
NegativeArraySizeException
11
NullPointerException
12
NumberFormatException
13
SecurityException
14
StringIndexOutOfBounds
15
UnsupportedOperationException
Pemrograman
Berorientasi Objek 1
Mengantisipasi Exception
Mendeklarasikan Exception
a.
Bentuk umum :
[modifier] returntype namaMethod() throws tipeException {
}
Contoh :
public void operasiMatematika() throws IOException,
ClassNotFoundException {
}
public void beriPinjaman() throws TolakException{
}
b.
Melempar Exception
Pemrograman
Berorientasi Objek 1
Bentuk umum :
TipeException namaObjek = new TipeException;
throw namaObjek;
Diringkas menjadi :
throw namaObjek TipeException;
atau
throw new TipeException();
Contoh :
TolakException t = new TolakException(lagi pelit);
throw t;
Diringkas menjadi :
throw new TolakException(lagi pelit!);
c. Menangkap Exception
Pemrograman
Berorientasi Objek 1
Bentuk umum :
try {
//pemanggilan method yg mungkin menghasilkan exception
}
catch(TipeException1 namaObjek) {
// penanganan salah-satu jenis exception
}
catch(TipeException2 namaObjek) {
// penanganan salah-satu jenis exception
}
catch(TipeExceptionN namaObjek) {
// penanganan salah-satu jenis exception
}
finally {
// blok yang harus dieksekusi
Pemrograman
Berorientasi Objek 1
Pemrograman
Berorientasi Objek 1
Pemrograman
Berorientasi Objek 1
Pemrograman
Berorientasi Objek 1
Deskripsi
getMessage()
toString()
getLocalizedMessage()
printStackTrace()
Pemrograman
Berorientasi Objek 1
class Eksepsi {
static void tampil(String s) throws MyException {
System.out.println("Tampil: " + s);
if (s.equals("amir"))
throw new MyException(s);
System.out.println("OK!");
}
public static void main(String argv[]) {
try {
tampil("ali");
System.out.println("Heh");
Output :
tampil("amir");
}
Tampil : ali
catch (MyException ex) {
OK!
System.out.println(ex);
Heh
}
Tampil : amir
finally {
MyExcepton : amir tidak diperbolehkan
System.out.println("OK");
OK
}
}
}