Working with Exception Classes

Objectives

After completing this lesson, you will be able to:

  • Work with exception classes

Exception Classes

Earlier in this learning journey, we looked at error handling using the TRY, CATCH, and ENDTRY statements. After the TRY statement comes the statement that might cause an error. If the error does occur, the system jumps to the CATCH statement and processes its contents. Whatever happens, the method carries on processing after the ENDTRY statement.

Sequence of CATCH Statements

When you write CATCH statements, you must place the most specific class (or classes) first and the most generic ones last. If you place a superclass above one of its subclasses, you will cause a syntax error. This is because the system could not process the specific CATCH block because it is overshadowed by the more generic one.

The INTO Addition

Each exception is represented by an object. You can place this object in a reference variable and work with it - for example, to retrieve the error text from the exception class using the get_text( ) method as shown in the example here.

You can use an inline declaration in the CATCH statement to ensure that the reference variable is declared with the correct type.

The most common thing to do with the exception object is to call its get_text( ) method. However, exception objects can possess other attributes and methods to help you handle the error.

The Attribute "Previous"

Sometimes within a call chain a method will catch an exception. In response, it will raise a different exception for its own caller to catch. This might happen, for example, if a framework raises an exception that is too technically-oriented for the application to process by itself. However, in certain circumstances the method that does this (method 2 in the example) may want to trigger its own exception, but also pass on the first exception. It can do this using the previous attribute of the exception class.

When you raise an exception, you can pass an instance of an exception class to the importing parameter previous. This attaches the exception object to the new exception. A method that catches the second exception can access the first exception object using the public read-only attributes previous.

Previous is implemented in the superclass CX_ROOT, and is therefore available in all exception classes.

Try It Out: Exception Classes

  1. Create a new class that implements the interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ).
    Code snippet
    
    DATA number1 TYPE i VALUE 2000000000.
    DATA number2 TYPE p LENGTH 2 DECIMALS 1 VALUE '0.5'.
    DATA result TYPE i.
    
    
    TRY.
    
    
    result = number1 / number2.
    
    
    CATCH cx_sy_arithmetic_overflow.
    out->write( 'Arithmetic Overflow' ).
    CATCH cx_sy_zerodivide.
    out->write( 'Division by zero' ).
    ENDTRY.
    
    
    number2 = 0.
    TRY.
    
    
    result = number1 / number2.
    
    
    CATCH cx_sy_arithmetic_overflow.
    out->write( 'Arithmetic Overflow' ).
    CATCH cx_sy_zerodivide.
    out->write( 'Division by zero' ).
    ENDTRY.
    
    TRY.
    result = number1 / number2.
    
    
    CATCH cx_sy_arithmetic_overflow cx_sy_zerodivide.
    out->write( 'Arithmetic overflow or division by zero' ).
    ENDTRY.
    
    TRY.
    result = number1 / number2.
    CATCH cx_sy_arithmetic_error.
    out->write( 'Caught both exceptions using their common superclass' ).
    ENDTRY.
    
    
    TRY.
    result = number1 / number2.
    CATCH cx_root.
    out->write( 'Caught any exception using CX_ROOT' ).
    ENDTRY.
    
    
    TRY.
    result = number1 / number2.
    CATCH cx_root into data(Exception).
    out->write( 'Used INTO to intercept the exception object' ).
    out->write( 'The get_Text( ) method returns the following error text: ' ).
    out->write( Exception->get_text( ) ).
    ENDTRY.
    Expand
  3. Activate the class with Ctrl + F3.
  4. Play around with the coding to familiarize yourself with it.

Log in to track your progress & complete quizzes