Вы находитесь на странице: 1из 17

PEMROGRAMAN

BERORIENTASI
OBYEK Session 8
Exception Handling

Koko Wahyu Prasetyo, S.Kom., M.T.I.


Semester Gasal 2012/2013
koko@stiki.ac.id
Exception Handling
Try - Catch - Finally

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Exceptions
 An event which occurs during the execution of a program
that disrupts the normal flow of the program

 When an error occurs within a method:


 The method creates an exception object and throws it
off to the runtime system

 Exceptions may be caused by:


 User’s error
 Programmer’s error
 Physical resource’s error

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Types of Exceptions
 Checked exceptions
 A problem that should be anticipated by the programmer
 i.e. java.io.FileNotFoundException

 Runtime exceptions
 A problem that are internal to the application and cannot
be anticipated by the program
 i.e. java.io.NullPointerException

 Errors
 A problem that are internal to the application and cannot
be anticipated by the program
 i.e. OutOfMemoryError
koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13
Catching Exceptions
 Use combination of try and catch keywords
 A try/catch block is placed around the code that
might generate an exception
 If an exception occurs in protected code, the
catch block that follows the try is executed

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Try Catch Example

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Multiple Catch Example

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Finally Keyword
A finally block of code always executes, whether
or not an exception has occurred
 Allows you to run any cleanup-type statements
that you want to execute, no matter what
happens in the protected code

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Try Catch Finally Example

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


About Try-Catch-Finally
A catch clause cannot exist without a try
statement
 It is not compulsory to have finally clauses
when ever a try/catch block is present
 The try block cannot be present without either
catch clause or finally clause
 Any code cannot be present in between the try,
catch, finally blocks

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Throwing Exceptions
 Ifa method does not handle a checked exception,
the method must declare it using the throws
keyword
 The throws keyword appears at the end of a
method's signature
 You can declare multiple exceptions after throws
keyword

 You can throw an exception, either a newly


instantiated one or an exception that you just
caught, by using the throw keyword

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Throws-Throw Example

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Make Your Own Exceptions
 You can create your own exceptions in Java with
following rules:
 If you want to write a checked exception that is
automatically enforced by the Handle or Declare
Rule, you need to extend the Exception class.
 If you want to write a runtime exception, you need
to extend the RuntimeException class.

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Example

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Example

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Example

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13


Let’s CODE!

koko@stiki.ac.id Pemrograman Berorientasi Obyek / S1 TI / Gasal 12-13