In this series, we look at the various factors that need to be considered when using class-based exceptions. The series is not an introduction to class-based exceptions. It assumes that the reader is familiar with the basics and has used class-based exceptions before. The intention of this series is to provide our own insights which would hopefully help the programming community make more informed choices with regards to exception handling. However, it should be pointed out that the content of this blog has also been influenced by various other blogs/articles written by other developers on this topic. Some of those have been listed below: -
The following are the various topics that we’ll look at in this blog series: -
In this blog, we’ll look at first topic – Why do we raise exceptions?
The principles of exception handling have been explained using the following ‘Library’ example: -
The library is a part of a university and has books which are available on loan to its members.
All students of the university are, however, not its members. Students need to be registered as members in the library before they can loan books.
The library is responsible for registering students as members. The library is also responsible for de-registration of students. However, de-registration must only be possible if books loaned by the student have been returned. The library is responsible for lending books that members want to borrow. It is also responsible for receiving books that members want to return. A book which has already been loaned-out cannot be loaned by another member unless it has been returned back to the library by the member. Additionally, the library does add new books to its stock or may remove existing books from its stock (if they have damaged). Each book is uniquely identified by its title (This is not realistic but we want to keep the example simple) and can have only copy.
The student must get himself registered as a member of the library if he wants to loan books from the library. Each student is uniquely identified by his name. The student can loan books and also return books back to the library. The student can get himself de-registered (provided he doesn’t have any un-returned books) from the library.
The following class diagram depicts the various classes: -
All program units (method/FM) generate outputs. By, outputs we are not only referring to the exporting and changing parameters of the program units, but also referring to any target (file, internal table, database table…etc.,) that the processed data could be written into.
The output has a domain i.e.., a list of possible values which indicate the result of execution of the program unit. If during the execution of the method/FM, a state is reached which cannot be adequately described by any value in the output domain, and as a result of which further execution cannot continue, we know that we have encountered an exceptional state. And, throwing an exception would be a good idea at this point.
This can be illustrated by an example program depicted by the following sequence diagram. Please note that some details have been omitted out for the sake of brevity: -
DATA: lv_current_title TYPE string,
lv_is_available TYPE boole_d.
DATA: lwa_book_loan TYPE wa_book_loan.
CALL METHOD is_book_available
EXPORTING
im_book = im_book
IMPORTING
ex_is_available = lv_is_available.
IF lv_is_available = abap_false.
RAISE EXCEPTION TYPE zcx_book_loans_exc.
ENDIF.
CALL METHOD find_loan_by_book
EXPORTING
im_book = im_book
IMPORTING
ex_book_loan = lwa_book_loan.
IF lwa_book_loan-student IS NOT INITIAL.
ex_is_loaned = abap_true.
ELSE.
ex_is_loaned = abap_false.
ENDIF.
Calling Program: -
TRY.
CALL METHOD lo_library->is_book_loaned
EXPORTING
im_book = lo_book3
IMPORTING
ex_is_loaned = lv_is_loaned.
IF lv_is_loaned = abap_true.
WRITE: /, lo_book3->get_title( ), ' has already been loaned-out'.
ELSE.
WRITE: /, lo_book3->get_title( ), ' is available to be loaned-out'.
ENDIF.
CATCH zcx_student_reg_dereg_excs.
WRITE: /, lo_book3->get_title(), 'is not available in the Library.
ENDTRY.
Exception handling has also been introduced at the points where the IS_BOOK_LOANED method was called for books lo_book1 (Catcher in the Rye)
and lo_book2 (The Kite Runner). Those code-snippets have not been included here for the sake of brevity.
The result of program execution after code changes is as follows: -
Catcher in the Rye has already been loaned-out
The Kite Runner is available to be loaned out
A Thousand Splendid Suns is not available in the library
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 |