Analyzing Runtime Errors

Objective

After completing this lesson, you will be able to analyze runtime errors

ABAP Short Dump

The Need for Debugging

There is no way around the fact that errors occur in programs. However, they manifest themselves in different ways. When a user starts a faulty application, it may crash, something unexpected may happen, or nothing at all may happen. From the user's point of view, at user interface level, it is impossible to say just how and why this error occurred.

As a developer, you now need to examine the program more closely, line-by-line in fact, to establish just what statements and combinations of values in the different program variables caused the error. This is where the Debugger comes in.

Runtime Errors

ABAP code containing a statement that will divide by zero

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.

ABAP Short Dump

Screenshot of a short dump with information about the error

If the runtime environment encounters a statement that 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. The runtime environment terminates the program if a runtime error is not caught. 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 while running the ABAP application, 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.

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

Screenshot of the “Navigate to source code” and “Share link to dump” buttons
Screenshot of the context menu after right-clicking on the source code in the short dump

There are three tabs at 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. The user may not understand a reference to the ABAP application and the presented technical information.

Avoiding Runtime Errors

Diagram showing the logical flow of a method with conditional branching in order to avoid a short dump
Example implementation of a method with conditional branching in order to avoid a short dump

In the above example, logic was added to avoid the division by zero runtime error.

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.

Note

In this exercise XX refers to your number.

Steps

  1. In your package, create a new ABAP class with the name, ZCL_XX_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_XX

    3. Enter the name, ZCL_XX_DUMP 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
      123456
      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 }| ).
  3. Activate and test your class.

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

    2. Run the class with the F9key.

    Result

    The program outputs the average of remaining vacation days per month.
  4. Change the Test program, 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_XX_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
      123456
      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.
    2. Now, activate and test the program.

      Code Snippet
      1234567891011121314151617181920212223242526
      * 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.

      Practice

Log in to track your progress & complete quizzes