PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 5, 2022

[FIXED] How to pass a local exception object to a class?

 August 05, 2022     abap, exception     No comments   

Issue

I want to pass an error local object in a class method which will display a detail error to the user.

This is the current code:

CATCH cx_root INTO lcx_general_error.
  DATA(lv_longtext) = lcx_general_error->get_longtext( ).
  lcx_general_error->get_source_position(
            IMPORTING
                program_name    = lv_program_name
                include_name    = lv_include_name
                source_line    = lv_program_line
                ).
  DATA(lv_program_include) = |{ lv_program_name }/ { lv_include_name }|.
  DATA(lv_length_message) = strlen( lv_longtext ).
  DATA(lv_error_message1) = lv_longtext(50).
  IF lv_length_message > 50.
    DATA(lv_remaining) = lv_length_message - 50.
    DATA(lv_error_message2) = lv_longtext+50(lv_remaining).
  ENDIF.

  MESSAGE e001 WITH lv_error_message1 lv_error_message2
                    lv_program_include
                    lv_program_line.

Instead, I want to create a class method and pass any local object that refers to any error and display the error detail message:

CATCH cx_root INTO lcx_general_error.
  lo_fi_uploads->display_error( lcx_general_error ).  

How to create and use this parameter in the local class?


Solution

Exceptions are regular classes with regular object instances, so declare them like any other object parameter:

METHODS display_error
  IMPORTING
    exception TYPE REF TO cx_root.

In the method’s implementation you can then paste the code you already have:

METHOD display_error.

  DATA(lv_longtext) = exception->get_longtext( ).
  exception->get_source_position(
        IMPORTING
            program_name    = DATA(lv_program_name)
            include_name    = DATA(lv_include_name)
            source_line    = DATA(lv_program_line)
            ).
  DATA(lv_program_include) = |{ lv_program_name }/ { lv_include_name }|.
  DATA(lv_length_message) = strlen( lv_longtext ).
  DATA(lv_error_message1) = lv_longtext(50).
  IF lv_length_message > 50.
    DATA(lv_remaining) = lv_length_message - 50.
    DATA(lv_error_message2) = lv_longtext+50(lv_remaining).
  ENDIF.

  MESSAGE e001 WITH lv_error_message1 lv_error_message2
                lv_program_include
                lv_program_line.

ENDMETHOD.

People often fear that working with exceptions might accidentally trigger them. That won’t happen. As long as you do not invoke the RAISE statement, exceptions are really quite ordinary objects. You can even instantiate them with NEW without triggering them.



Answered By - Florian
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing