24
Do you remember this?

Do you remember this?

  • Upload
    ada

  • View
    31

  • Download
    1

Embed Size (px)

DESCRIPTION

Do you remember this?. Strategy Pattern consists of. Strategy Concrete Strategy Context Client. strategy. Do you remember this?. c oncrete strategy. c ontext. Client?. Refer to sample codes. Exception handling. Tim Praktikum PSBO 08/09. Exception. - PowerPoint PPT Presentation

Citation preview

Page 1: Do you remember this?

Do you remember this?

Page 2: Do you remember this?

Strategy Concrete Strategy Context Client

Strategy Pattern consists of..

Page 3: Do you remember this?

Do you remember this?

context

strategy

concrete strategy

Client?

Page 4: Do you remember this?

Refer to sample codes..

Page 5: Do you remember this?

EXCEPTION HANDLING

Tim Praktikum PSBO 08/09

Page 6: Do you remember this?

Exception

Exception suatu objek yang dibuat pada saat program mengalami suatu kondisi yang tidak wajar (abnormal)

Dalam java, exception dapat dibangkitkan secara otomatis oleh sistem atau secara manual oleh kita sendiri melelui kode yang ditulis.

Page 7: Do you remember this?

Kesalahan yang terjadi :

Pembagian bilangan integer dengan 0 Pengisian elemen array diluar ukuran

array Kegagalan koneksi database File yang akan dibuka tidak exist Operand yg akan dimanipulasi out of

prescribed range Mengakses obyek yang belum

diinisialisasi

Page 8: Do you remember this?

Contoh

Pembagian dengan bilangan integer

class DivByZero {

public static void main(String args[]) {

System.out.println(3/0);

System.out.println(“Cetak.”);

}

}

Program akan jalan?

Page 9: Do you remember this?

Kalaupun di run akan mendapat pesan

How to handle that?Exception handling using try, catch, finally method.

Page 10: Do you remember this?

Try…

Try blok dimana kode program memberitahukan kepada compiler bahwa suatu method akan terjadi exception

Blok try : digunakan untuk menempatkan kode-kode program java yang mengandung kode program yang mungkin melemparkan exception

Page 11: Do you remember this?

Contoh

try{

System.out.println(3/0);

System.out.println("Cetak.");

}

Type exception: ArithmeticException

Page 12: Do you remember this?

Catch…

Catch merupakan pasangan dari try Ketika try mengetahui tipe exception yang

terjadi catch berfungsi untuk menghandle lemparan kesalahan dari try

Page 13: Do you remember this?

contoh

catch(ArithmeticException exc) {

System.out.println("salah coy soale dibagi nol");

}

Catch : Jika kita menangkap exception dengan type exception yang tidak sesuai maka exception handling tidak dapat dijalankan

Page 14: Do you remember this?

Contoh Class

public class Exception {

public static void main(String args[]) {

try {

System.out.println(3/0);

System.out.println("Cetak.");

}

catch(ArithmeticException exc) {

System.out.println(“Error : Divided by zero!");

}

catch(ArrayIndexOutOfBoundsException exc2) {

System.out.println("Missing argument.");

}

System.out.println("Setelah Exception.");

}

}

Page 15: Do you remember this?

Outputnya :Error : Divided by zero!

Setelah Exception

Page 16: Do you remember this?

Contoh Class

public class Exception {

public static void main(String args[]) {

try {

System.out.println(3/0);

System.out.println("Cetak.");

}

catch(ArithmeticException exc) {

System.out.println(“Error : Divided by zero!");

}

catch(ArrayIndexOutOfBoundsException exc2) {

System.out.println("Missing argument.");

}

System.out.println("Setelah Exception.");

}

}

Page 17: Do you remember this?

Namun catch bisa di buat lebih dari satu dengan asumsi try akan melempar lebih dari satu exception handling dengan tipe yang berbeda. Contoh :

Page 18: Do you remember this?

try {Fungsi bacaFileBukaFileBacaBarisFileSampaiHabisTutupFile

} catch (KesalahanBukaFile) {// lakukan sesuatu

} catch (KesalahanAlokasiMemori) {// lakukan sesuatu

} catch (KesalahanTutupFile) {// lakukan sesuatu

}

Perbedaan exception

yang ditangkap

Page 19: Do you remember this?

Finally…

Blok finally : digunakan untuk mendefinisikan kode program yang selalu dieksekusi baik ada exception yang terjadi maupun bila tidak terjadi exception sama sekali.

Bersifat optional

Page 20: Do you remember this?

public class FinallyDemo {

public static void main(String[] args){

int i=0;

String[] greetings = {

"hello pagi", "cagur", "test lagi"

};

while (i<4){

try{

System.out.println(greetings[i]);

i++;

}

catch(ArrayIndexOutOfBoundsException e){

System.out.println("indeks value");

i++;

//i=0;

}

finally{

System.out.println("finall");

}

}

}

}

Page 21: Do you remember this?

Output :hello pagi -> i=0

finall

Cagur -> i=1

finall

test lagi -> i=2

finall

indeks value -> exception

finall

--------Ketika ada exception atau tidak finall tetap akan keluar.

Output jika I di reset menjadi 0 ??

Page 22: Do you remember this?

Diletakkan setelah statement catch Hampir pasti dijalankan

Tidak dijalankan jika program keluar pada blok try

System.exit Biasanya digunakan untuk kode

resource-release Menutup koneksi database Menutup koneksi jaringan

Page 23: Do you remember this?

throw

Selain menangkap, java juga mengizinkan seorang user untuk melempar sebuah exception.

Kata kunci throw <exception object>;

Page 24: Do you remember this?

throws

Jika sebuah method dapat menyebabkan sebuah exception namun tidak menangkapnya, maka digunakan keyword throws. Aturan ini hanya berlaku pada checked exception.

kata kunci <type> <methodName>

(<parameterList>) throws <exceptionList> {<methodBody>}