1 / 43

Exception Handling

Exception Handling. Oleh : Nur Hayatin , S.ST ( Sumber : Tita Karlita , S.Kom & Jeni 1). tujuan. Pada akhir bab, diharapkan peserta mampu untuk : Mendefinisikan exception. Menangani exception dengan menggunakan blok try-catch-finally sederhana. Kesalahan yg muncul.

fawzia
Télécharger la présentation

Exception Handling

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. Exception Handling Oleh : NurHayatin, S.ST (Sumber : TitaKarlita, S.Kom & Jeni 1)

  2. tujuan Pada akhir bab, diharapkan peserta mampu untuk : • Mendefinisikan exception. • Menangani exception dengan menggunakan blok try-catch-finally sederhana.

  3. Kesalahanygmuncul

  4. PembagiandenganNol (TanpaException) class DivByZero { public static void main(String args[]) { System.out.println(3/0); System.out.println(“Pls. print me.”); } } • didapatkan pesan kesalahan sebagai berikut • Exception in thread "main" java.lang.ArithmeticException: / by zero at DivByZero.main(DivByZero.java:3)

  5. class DivByZero { public static void main(String args[]) { try { System.out.println(3/0); System.out.println(“Please print me.”); } catch (ArithmeticExceptionexc) { //Dibagidengan 0 -> ArithmeticException System.out.println(exc); } System.out.println(“After exception.”); } } PembagiandenganNol(Dengan Exception)

  6. Contoh : Multiple Catch Contoh Multiple catch class MultipleCatch { public static void main(String args[]) { try { int den = Integer.parseInt(args[0]); System.out.println(3/den); } catch (ArithmeticExceptionexc) { System.out.println(“Divisor was 0.”); } catch (ArrayIndexOutOfBoundsException exc2) { System.out.println(“Missing argument.”); } System.out.println(“After exception.”); } }

  7. Exceptions Catch: try-catch Nested try class NestedTryDemo { public static void main(String args[]){ try { int a = Integer.parseInt(args[0]); try { int b = Integer.parseInt(args[1]); System.out.println(a/b); } catch (ArithmeticException e) { System.out.println(“Div by zero error!"); } //bersambung...

  8. Exceptions Catch: try-catch } catch (ArrayIndexOutOfBoundsException e) { System.out.println(“Need 2 parameters!"); } } }

  9. Exceptions Catch: try-catch Nested trys dengan method-method class NestedTryDemo2 { static void nestedTry(String args[]) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println(a/b); } catch (ArithmeticException e) { System.out.println("Div by zero error!"); } } //bersambung...

  10. Exceptions Catch: try-catch public static void main(String args[]){ try { nestedTry(args); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Need 2 parameters!"); } } }

  11. Alur program

  12. Blok try-catch-finally Hal-hal yang perlu diperhatikan saat membuat sintak try-catch-finally : • Wajib membuat notasi blok • Setiap blok try boleh memiliki lebih dari satu blok catch dan hanya boleh memiliki satu blok finally • Blok catch dan blok finally harus muncul bersama blok try • Blok try harus diikuti minimal satu blok catch, atau satu blok finally, atau kedua blok catch dan finally • Setiap blok catch mendefinisikan penanganan exception. Di dalam header blok catch terdapat satu argumen yang akan ditangani oleh blok exception. Exception harus berasal dari class Throwable atau dari class turunannya

  13. Exceptions Throwing : Keyword throws Sebuah method diperlukan untuk menangkap ataupun mendaftar seluruh exceptions yang mungkin terjadi Kecuali untuk Error atau RuntimeException, atau subclass-subclass nya Jika sebuah method dapat menyebabkan sebuah exception namun tidak menangkapnya, maka digunakan keyword throws Aturan ini hanya berlaku pada checked exception Sintaks: <type> <methodName> (<parameterList>) throws <exceptionList> { <methodBody> }

More Related