Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

RAISE Exception

former_member184495
Active Contributor
0 Kudos
621

Hi,

a novice in ABAP OOP's. I am using FM CATSXT_TEXT_EDITOR which when executed displays a text area, where we can enter text.

Now my requirement is that I want to set a check, where I want to tell the user to enter something and not leave the text area empty.

How could I achieve this?

Do I need to use any RAISE exceptions?

say, my code stores the text that I entered in a variable ch_text.

*Stores text

ch_text = ste->get_text()

*

Now how do I achieve this checking of whether ch_text is empty, if empty RAISE an exception.

Aditya

1 ACCEPTED SOLUTION

MarcinPciak
Active Contributor
0 Kudos
313

This actually depends on what further processing do you expect if the user don't enter any text. Also are you going to get back to application or leave it entirely showing the user certain error message. I don't think you want to dump the program once there is no text in the text area. Using RAISE EXCEPTION without capturing this would cause such behaviuor.

Generally I think simple validation as below is enough


if ch_text is initial.
   .... "do some sort of action like leaving the program or showing the message
  MESSAGE 'Sorry you left the text area empty, please correct' TYPE 'I' display as 'E'.
endif.

As for the RAISE itself you can catch the error it is raising and show the user either self-constructed message or one from some standard exception class. For this you need an exception class either local or global as the TYPE of exception. With local class this could be achieved such way


"class must inherit from CX_DYNAMIC_CHECK or CX_STATIC_CHECK or CX_NO_CHECK

CLASS lcx_validation DEFINITION INHERITING FROM cx_dynamic_check.  
  PUBLIC SECTION.
    METHODS: constructor,
             get_text REDEFINITION.

  PRIVATE SECTION.
    DATA: lcx_validation TYPE string VALUE
          'Text area is empty, please correct that'.
ENDCLASS.                   

CLASS lcx_validation IMPLEMENTATION.
  METHOD constructor.
    super->constructor( ).
  ENDMETHOD.          

  METHOD get_text.
    result = me->lcx_validation .
  ENDMETHOD.              
ENDCLASS.                 

"define reference to this excpetion class
data grx_valition type ref to lcx_validation.
data g_mess type string.

"now in code you can raise exception of this class
TRY.
    if ch_text is initial.
     RAISE EXCEPTION TYPE lcx_validation.  "raise the exception 
   endif.
   CATCH lcx_valition INTO grx_validation.
       g_mess = grx_validation->get_text( ).
        MESSAGE g_mess TYPE 'I' DISPLAY LIKE 'E'.
        EXIT.
ENDTRY.

Exceptions are rather useful for different application components to report that some inconsistent state of object occured. Usually this is raised in some method, then propagated up in call hierarchy to the caller and there handled appropriatetly. So basically it is more for comunication b/w differents objects than for pure validation especially such basic one. Anyhow you can do this in such sopthisitacted way but in my opinion you gain nothing.

Regards

Marcin

3 REPLIES 3

MarcinPciak
Active Contributor
0 Kudos
314

This actually depends on what further processing do you expect if the user don't enter any text. Also are you going to get back to application or leave it entirely showing the user certain error message. I don't think you want to dump the program once there is no text in the text area. Using RAISE EXCEPTION without capturing this would cause such behaviuor.

Generally I think simple validation as below is enough


if ch_text is initial.
   .... "do some sort of action like leaving the program or showing the message
  MESSAGE 'Sorry you left the text area empty, please correct' TYPE 'I' display as 'E'.
endif.

As for the RAISE itself you can catch the error it is raising and show the user either self-constructed message or one from some standard exception class. For this you need an exception class either local or global as the TYPE of exception. With local class this could be achieved such way


"class must inherit from CX_DYNAMIC_CHECK or CX_STATIC_CHECK or CX_NO_CHECK

CLASS lcx_validation DEFINITION INHERITING FROM cx_dynamic_check.  
  PUBLIC SECTION.
    METHODS: constructor,
             get_text REDEFINITION.

  PRIVATE SECTION.
    DATA: lcx_validation TYPE string VALUE
          'Text area is empty, please correct that'.
ENDCLASS.                   

CLASS lcx_validation IMPLEMENTATION.
  METHOD constructor.
    super->constructor( ).
  ENDMETHOD.          

  METHOD get_text.
    result = me->lcx_validation .
  ENDMETHOD.              
ENDCLASS.                 

"define reference to this excpetion class
data grx_valition type ref to lcx_validation.
data g_mess type string.

"now in code you can raise exception of this class
TRY.
    if ch_text is initial.
     RAISE EXCEPTION TYPE lcx_validation.  "raise the exception 
   endif.
   CATCH lcx_valition INTO grx_validation.
       g_mess = grx_validation->get_text( ).
        MESSAGE g_mess TYPE 'I' DISPLAY LIKE 'E'.
        EXIT.
ENDTRY.

Exceptions are rather useful for different application components to report that some inconsistent state of object occured. Usually this is raised in some method, then propagated up in call hierarchy to the caller and there handled appropriatetly. So basically it is more for comunication b/w differents objects than for pure validation especially such basic one. Anyhow you can do this in such sopthisitacted way but in my opinion you gain nothing.

Regards

Marcin

0 Kudos
313

Hi MP,

actually, I should give him an error message saying Text is empty and also shouldnt leave the application.

Aditya

0 Kudos
313

Then all the above applies to you, expect the EXIT command in the CATCH block. So I am not sure where your confusion lies.

Regards

Marcin