1 / 24

Do you remember this?

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.

ada
Download Presentation

Do you remember this?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Do you remember this?

  2. Strategy Pattern consists of.. Strategy Concrete Strategy Context Client

  3. strategy Do you remember this? concrete strategy context Client?

  4. Refer to sample codes..

  5. Exception handling Tim Praktikum PSBO 08/09

  6. Exception • Exception  suatuobjek yang dibuatpadasaat program mengalamisuatukondisi yang tidakwajar (abnormal) • Dalamjava, exception dapatdibangkitkansecaraotomatisolehsistematausecara manual olehkitasendirimeleluikode yang ditulis.

  7. Kesalahanyang terjadi : • Pembagianbilangan integer dengan 0 • Pengisianelemen array diluarukuran array • Kegagalankoneksi database • File yang akandibukatidak exist • Operand ygakandimanipulasi out of prescribed range • Mengaksesobyek yang belumdiinisialisasi

  8. Contoh • Pembagiandenganbilangan integer class DivByZero { public static void main(String args[]) { System.out.println(3/0); System.out.println(“Cetak.”); } } Program akanjalan?

  9. Kalaupundi run akanmendapatpesan • How to handle that? Exception handling using try, catch, finally method.

  10. Try… • Try  blokdimanakode program memberitahukankepadacompiler bahwasuatu method akanterjadiexception • Blok try : digunakanuntukmenempatkankode-kode program java yang mengandungkode program yang mungkinmelemparkan exception

  11. Contoh try{ System.out.println(3/0); System.out.println("Cetak."); } • Type exception: ArithmeticException

  12. Catch… • Catch merupakanpasangandari try • Ketika try mengetahuitipe exception yang terjadi catch berfungsiuntukmenghandlelemparankesalahandari try

  13. contoh catch(ArithmeticExceptionexc) { System.out.println("salah coy soaledibaginol"); } • Catch : Jikakitamenangkap exception dengan type exception yang tidaksesuaimaka exception handling tidakdapatdijalankan

  14. Contoh Class public class Exception { public static void main(String args[]) { try{ System.out.println(3/0); System.out.println("Cetak."); } catch(ArithmeticExceptionexc) { System.out.println(“Error : Divided by zero!"); } catch(ArrayIndexOutOfBoundsExceptionexc2) { System.out.println("Missing argument."); } System.out.println("Setelah Exception."); } }

  15. Outputnya : Error : Divided by zero! Setelah Exception

  16. Contoh Class public class Exception { public static void main(String args[]) { try{ System.out.println(3/0); System.out.println("Cetak."); } catch(ArithmeticExceptionexc) { System.out.println(“Error : Divided by zero!"); } catch(ArrayIndexOutOfBoundsExceptionexc2) { System.out.println("Missing argument."); } System.out.println("Setelah Exception."); } }

  17. Namun catch bisadibuatlebihdarisatudenganasumsi try akanmelemparlebihdarisatu exception handling dengantipe yang berbeda. Contoh :

  18. try { FungsibacaFile BukaFile BacaBarisFileSampaiHabis TutupFile } catch (KesalahanBukaFile) { // lakukansesuatu } catch (KesalahanAlokasiMemori) { // lakukansesuatu } catch (KesalahanTutupFile) { // lakukansesuatu } Perbedaan exception yang ditangkap

  19. Finally… • Blok finally : digunakanuntukmendefinisikankode program yang selaludieksekusibaikada exception yang terjadimaupunbilatidakterjadi exception samasekali. • Bersifat optional

  20. public class FinallyDemo { public static void main(String[] args){ inti=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"); } } } }

  21. Output : hello pagi -> i=0 finall Cagur -> i=1 finall test lagi -> i=2 finall indeks value -> exception finall --------Ketikaada exception atautidakfinalltetapakankeluar. • Output jika I di reset menjadi 0 ??

  22. Diletakkansetelah statement catch • Hampirpastidijalankan • Tidakdijalankanjika program keluarpadablok try • System.exit • Biasanyadigunakanuntukkode resource-release • Menutupkoneksi database • Menutupkoneksijaringan

  23. throw • Selainmenangkap, java jugamengizinkanseorang user untukmelemparsebuah exception. • Katakunci • throw <exception object>;

  24. throws • Jikasebuah method dapatmenyebabkansebuah exception namuntidakmenangkapnya, makadigunakan keyword throws. Aturaninihanyaberlakupadachecked exception. • katakunci • <type> <methodName> (<parameterList>) throws <exceptionList> { <methodBody> }

More Related