8/21/2019 Materi Exception
1/20
1
I Ketut Putra Yasa
Exception
Pemrograman Berorientasi
Objek I
8/21/2019 Materi Exception
2/20
2
Exception Dalam Java, runtime error(kesalahan-kesalahan yang terjadi pada saat
program sedang berjalan) direpresentasikan dengan exception.Eksepsi(Exception) adalah suatu objek yang dibuat pada saat program
mengalami suatu kondisi yang tidak wajar(abnormal). Ada beberapa contoh dari exception :
>> ArrayIndexOutOfBounds Exception
Terjadi pada saat pengaksesan elemen array yang tidak ada.
>> NumberFormat Exception
Terjadi ketika mencobapassingsebagai parameter bukan angka
dalam method Integer.parseInt
8/21/2019 Materi Exception
3/20
3
Contoh Exception1. publ i c cl ass Di vByZer o {
2. publ i c st at i c voi d mai n( St r i ng[ ] ar gs) {
3. i nt angka1 = 1;
4. i nt angka2 = 0;
5. i nt hasi l = angka1/ angka2; //Salah
6. Syst em. out . pr i nt l n( "Hasi l =" + hasi l ) ;
7. }
8. }
Didapatkan pesan kesalahan sebagai berikut :Except i on i n t hr ead "mai n"j ava. l ang. Ar i t hmet i cExcept i on: / by zer o
at Di vByZer o. mai n( Di vByZer o. j ava: 5)
8/21/2019 Materi Exception
4/20
4
Exceptions Catch: try-catch
Sintaks:try {
} cat ch ( ) {
}
. . .} cat ch ( ) {
}
8/21/2019 Materi Exception
5/20
5
Exceptions Catch: try-catchpubl i c cl ass Di vByZer o {publ i c stat i c voi d mai n( St r i ng[ ] ar gs) {
i nt angka1 = 1;i nt angka2 = 0;
try{i nt hasi l = angka1/ angka2;Syst em. out . pr i nt l n( "Hasi l =" + hasi l ) ;
}cat ch( Ar i t hmet i cExcept i on ae) {Syst em. out . pr i nt l n( "KESALAHAN : "+"Pembagi nya Ti dak Bol eh Nol " ) ;
}}
}
Output :KESALAHAN : Pembagi nya Ti dak Bol eh Nol
8/21/2019 Materi Exception
6/20
6
Exceptions Catch: try-catch Contoh Multiple catch
publ i c cl ass Mul t i pl eCat ch {publ i c st at i c voi d hi t ung( i nt x, i nt y) {
try{i nt c = x/ y;Syst em. out . pr i nt l n( "Hasi l =" +c) ;
i nt [ ] ni l ai ={1, 2, 3, 4, 5};ni l ai [ 5] = 10;
}cat ch( Ar i t hmet i cExcept i on ae) {Syst em. out . pr i nt l n( "Pembagi nya Ti dak Bol eh 0") ;Syst em. out . pr i nt l n( ae. get Message( ) ) ;
/ / ber sambung
8/21/2019 Materi Exception
7/207
Exceptions Catch: try-catch}cat ch( Ar r ayI ndexOut Of BoundsExcept i on be) {
Syst em. out . pr i nt l n( " I ndek di Luar Rent ang") ;Syst em. out . pr i nt l n( be. get Message( ) ) ;
}}publ i c stat i c voi d mai n( St r i ng[ ] ar gs) {
hi t ung( 4, 0) ; / / Ar i t hmet i cExcept i onSyst em. out . pr i nt l n( ) ;hi t ung( 4, 2) ; / / Ar r ayI ndexOut Of BoundsExcept i on
}}
Output :Pembagi nya Ti dak Bol eh 0/ by zer oHasi l =2I ndek di Luar Rent ang
5
8/21/2019 Materi Exception
8/208
Exceptions Catch: Keywordfinally Sintaks:
try {
} cat ch ( ) {
} . . .
} f i nal l y {
}
Mengandung kode penanganan setelah penggunaan trydan catch
8/21/2019 Materi Exception
9/209
Exceptions Catch: Keywordfinally
Kode selalu dieksekusi meskipun dengan skenario berbeda :
>> Proses keluar secara paksa menggunakan return, pernyataan
continueatau break.
>> Penyelesaian secara normal
> Menangkap lemparan exception
-> Exception akan dilempar dan ditangkap dalam sebuahmethod
8/21/2019 Materi Exception
10/2010
Exceptions Catch: Keywordfinallypubl i c cl ass DemoFi nal l y {
pr i vat e stat i c i nt i = 0;
publ i c st at i c voi d mai n( St r i ng[ ] ar gs) {
whi l e( t r ue) {try{
Syst em. out . pr i nt ( "Pada Saat i ="+i +" "+": ") ;i f ( i ++ == 0) {
t hr ow new Except i on( ) ; / / Mel empar except i on
}Syst em. out . pr i nt l n( "Ti dak Ter j adi Except i on" ) ;
/ / ber sambung
8/21/2019 Materi Exception
11/20
11
Exceptions Catch: Keywordfinally}cat ch( Except i on e) {
Syst em. out . pr i nt l n( "Ter j adi Except i on") ;}f i nal l y{
Syst em. out . pr i nt l n( "St at emen Bl ok Fi nal l y\ n") ;i f ( i == 2) {
br eak; / / i =2 per ul angan ber hent i}
}}
}}
Output :Pada Saat i =0 : Ter j adi Except i onSt at emen Bl ok Fi nal l y
Pada Saat i =1 : Ti dak Ter j adi Except i onSt at emen Bl ok Fi nal l y
8/21/2019 Materi Exception
12/20
12
Exceptions Throwing : Keyword throw
Java mengijinkan Anda untuk melempar exception:
t hr ow ;
Contoh:
t hr ow new
Ar i t hmet i cExcept i on( t est i ng. . . ) ;
8/21/2019 Materi Exception
13/20
13
Exceptions Throwing : Keyword throwcl ass Thr owDemo {
publ i c st at i c voi d mai n( St r i ng ar gs[ ] ) {St r i ng i nput = "nama";
try {i f ( i nput . equal s( "nama") ) {
t hr ow new Runt i meExcept i on( " t hrow demo") ;} el se {
Syst em. out . pr i nt l n( i nput ) ;}
Syst em. out . pr i nt l n( "Af t er t hr owi ng" ) ;
} cat ch ( Runt i meExcept i on e) {Syst em. out . pr i nt l n( "Except i on caught : "+e) ;
}}
}
8/21/2019 Materi Exception
14/20
14
Exceptions Throwing : Keyword throws Sebuah method diperlukan untuk menangkap ataupun mendaftar seluruh
exceptions yang mungkin terjadi
Kecuali untuk Erroratau RuntimeException, atau subclass-subclass nya
Jika sebuah method dapat menyebabkan sebuah exception namun tidakmenangkapnya, maka digunakan keyword throws
Aturan ini hanya berlaku pada checked exception
Sintaks: ( ) throws {
}
8/21/2019 Materi Exception
15/20
15
Exceptions Throwing : Keyword throwscl ass Thr owi ngCl ass {
st at i c voi d met h( ) t hr ows Cl assNot FoundExcept i on {t hr ow new Cl assNot FoundExcept i on ( "demo") ;
}}cl ass ThrowsDemo {
publ i c st at i c voi d mai n( St r i ng ar gs[ ] ) {try {
Thr owi ngCl ass. met h( ) ;
} cat ch ( Cl assNot FoundExcept i on e) {Syst em. out . pr i nt l n( e) ;
}}
}
8/21/2019 Materi Exception
16/20
16
Class-class Exception dan Hirarki
8/21/2019 Materi Exception
17/20
17
Class-class Exception dan Hirarki catch lebih dari satu harus berurutan dari subclass ke superclass.
cl ass Mul t i pl eCat chEr r or {publ i c st at i c voi d mai n( St r i ng ar gs[ ] ) {
try {i nt a = 2/ 0;Syst em. out . pr i nt l n( a) ;
} cat ch ( Ar i t hmet i cExcept i on e) {Syst em. out . pr i nt l n( e. get Message( ) ) ;
} cat ch ( Except i on ex) {Syst em. out . pr i nt l n( ex. get Message( ) ) ;}
}}
8/21/2019 Materi Exception
18/20
18
User-Defined Exceptions Membuat exceptions Anda sendiri
Membuat sebuah class yang extendsterhadap RuntimeExceptionatau classException
Modifikasi class
Members dan constructors dapat ditambahkan pada class
Contoh:
cl ass Hat eSt r i ngExp ext ends Runt i meExcept i on {
/ * Ti dak ada penambahan anggota
*at au const r ukt or sedi ki t pun */
}
8/21/2019 Materi Exception
19/20
19
User-Defined Exceptionscl ass Test Hat eSt r i ng {publ i c st at i c voi d mai n( St r i ng ar gs[ ] ) {
St r i ng i nput = "i nval i d i nput ";
try {
i f ( i nput . equal s( " i nval i d i nput ") ) {
t hr ow new Hat eSt r i ngExp ( ) ;
}
Syst em. out . pr i nt l n( i nput ) ;
} cat ch ( Hat eSt r i ngExp e) {
Syst em. out . pr i nt l n( Eksepsi St r i ng! ") ;}
}
}
8/21/2019 Materi Exception
20/20
20
Referensi :Sumber : Module JENI 1
Top Related