Analyzing Runtime Errors

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Implement conditional logic and use it in a program

Error Handling

Conditional Branches

Analyze Runtime Errors

ABAP Short Dump

It is important to perform a syntax check regularly when writing ABAP code. However, correct syntax does not always mean your program will execute successfully at runtime.

For example, calculations in your program may cause a problem, depending on the values used. The figure, A Syntactically Correct Program Can Have Problems, demonstrates a program that, while syntactically correct, will cause a runtime error.

If the runtime environment encounters a statement which cannot be executed, it terminates the program and triggers a runtime error. Each runtime error is identified by a name and assigned to a specific error situation. If a runtime error is not caught, the runtime environment terminates the program. The ABAP Runtime Error can be displayed in the ABAP Runtime Error Viewer as of ABAP 7.53.

The ABAP Runtime Error Viewer can be opened using different entry points:

  • If the error occurs when you are running the ABAP program, a dialog appears in the bottom-right corner. Choose Show to display the error.
  • In the Feed Reader view, double-click on the runtime error entry.
  • If a runtime error occurs during an ABAP Unit test, choose the Show Runtime Error in the Failure Trace section of the ABAP Unit view.

From the toolbar, you can navigate to the source code position where the dump occurred and share the link to the dump.

There are three tabs on the bottom of the editor:

  1. The first provides the following information:

    • Header information
    • Error analysis
    • Information on where the program terminated
    • Source code extract
    • The line in which the error occurred is highlighted; choose the line to navigate to the error in the source code
    • Active calls/events
  2. The Long Text tab provides the full dump information. It supports the features, Outline and Quick Outline (Ctrl+ O on your keyboard).

  3. The Unformatted Display tab provides the dump information in a technical format that is only necessary for special analysis situations.

A short dump also allows you to navigate directly to the ABAP Debugger to see the values of variables just before the runtime error occurred.

Runtime errors are normal. You will probably encounter them frequently while developing your programs. However, it is important that you try to prevent them. A user in your production system may be confused if they see a runtime error such as the error in the figure, since the user may not understand a reference to the ABAP program and the technical information that is presented.

Avoiding Runtime Errors

A short dump also allows you to navigate directly to the ABAP Debugger to see the values of variables just before the runtime error occurred.

Use Conditional Logic to Avoid a Runtime Error

Business Example

One of your colleagues created a program that calculates the average number of vacation days remaining for each month left in the year. Another colleague mentioned that the program performs strangely when there are no months remaining in the year. Test the program and make any changes necessary to ensure that it works in all circumstances.

Prerequisites

Steps

  1. In your package, create a new ABAP class with the name, ZCL_##_DUMP. Ensure that it uses the interface, IF_OO_ADT_CLASSRUN. When you are prompted to assign the class to a transport request, use the transport request that you created in the previous task.

    1. Choose FileNewABAP Class.

    2. Enter your package, ZS4D100_##, where ## is your group number. 

    3. Enter the name, ZCL_##_DUMP, where ## is your group number and enter a description for your class.

    4.  Choose Add... (next to the Interfaces group box).

    5. Enter the filter text, IF_OO_ADT_CLASSRUN. Double-click the matching entry in the hit list.

    6. Choose Next.

    7. Select Choose from requests in which I am involved and your own transport request.

    8. Choose Finish.

  2. In the if_oo_adt_classrun~main( ) add the following code:

    1. In the editor, enter the following coding between METHOD if_oo_adt_classrun~main and ENDMETHOD.: .

      Code snippet
      DATA: lv_mths TYPE i VALUE 10, "No. of months remaining in year
            lv_hols TYPE i VALUE 20, "No. of days untaken vacation
            lv_avg_hols TYPE i.
      
      lv_avg_hols = lv_hols / lv_mths.
      out->write( |Average vacation days per month { lv_avg_hols }| ).
      Copy code
  3. Activate and test your class.

    1. Activate the class with the keyboard shortcut Ctrl + F3.

    2. Run the class with the F9 key.

    Result

    The program outputs the average of remaining vacation days per month.
  4. Change the Test the program and check the result if there are 0 months remaining in the year, and the employee has 20 unused vacation days.

    1. Set the value of lv_mths to 0.

    2. Choose Activate (Ctrl+F3).

    3. Run the program by choosing F9.

    Result

    The program terminates and displays the details of a runtime error.
  5. Analyze the runtime error to identify the problem. Why did the error occur?

    1. Run the program by choosing F9.

    2. Eclipse will display a pop up window in the bottom right with the title ABAP Runtime Error.

    3. Click on the Show link to analyze the error.

    4. Page down to see what happened and which line caused the error.

    The error occurred because it is mathematically impossible to divide by zero.

  6. Switch to the ABAP Editor.

    1. Choose the tab, ZCL_S4D100_##_DUMP.

  7. To prevent the runtime error from occurring, implement a condition so that the program outputs the following text if there are no months remaining: Please ask if you can roll over your remaining vacation. Otherwise, perform the calculation and output the result as before.

    1. Add the following code around the existing calculation:

      Code snippet
      IF lv_mths NE 0.
            lv_avg_hols = lv_hols / lv_mths.
            out->write( |Average vacation days per month { lv_avg_hols }| ).
      ELSE.
            out->write( |Please ask to roll over remaining vacation| ).
      ENDIF.
      Copy code
    2. Now, activate and test the program.

      Code snippet
      * Full Solution Code
      
      CLASS zcl_s4d100_dump DEFINITION PUBLIC FINAL CREATE PUBLIC .
      
      PUBLIC SECTION.
        INTERFACES if_oo_adt_classrun .
      PROTECTED SECTION.
      PRIVATE SECTION.
      ENDCLASS.
      
      CLASS zcl_s4d100_dump IMPLEMENTATION.
      
        METHOD if_oo_adt_classrun~main.
          DATA: lv_mths TYPE i VALUE 0, "No. of months remaining in year
                lv_hols TYPE i VALUE 10, "No. of days untaken vacation
                lv_avg_hols TYPE i.
      
          IF lv_mths <> 0.
            lv_avg_hols = lv_hols / lv_mths.
            out->write( |Average vacation days per month { lv_avg_hols }| ).
          ELSE.
            out->write( |Please ask to roll over remaining vacation!| ).
           ENDIF.
      
      ENDMETHOD.
      ENDCLASS.
      Copy code

      Practice

Save progress to your learning plan by logging in or creating an account