Application Development and Automation 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: 
Read only

obligatory endcatch?

Former Member
0 Likes
240

the next code:

***********************

CLASS bank_application IMPLEMENTATION.

METHOD main.

DATA: account1 TYPE REF TO account,

account2 TYPE REF TO account,

amnt TYPE i,

exc_ref TYPE REF TO cx_negative_amount,

text TYPE string.

CREATE OBJECT: account1 EXPORTING id = `...`,

account2 EXPORTING id = `...`.

TRY.

amnt = ...

account1->transfer( EXPORTING amount = amnt

target = account2 ).

CATCH cx_negative_amount INTO exc_ref.

text = exc_ref->get_text( ).

MESSAGE text TYPE 'I'.

ENDTRY.

ENDMETHOD.

ENDCLASS.

*******************************

the statement endcatch? Why?

Is used the statement endcatch when I want continue type code after <b>"MESSAGE text TYPE 'I'."</b>?

cordial greetings

1 REPLY 1
Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
218

Hi Lopes,

Look at example <a href="https://wiki.sdn.sap.com/wiki/display/Snippets/ABAPObjects-DefiningaClass-basedexceptions">ABAP Objects - Defining a Class-based exceptions</a>.

When you're using exception class you don't use ENDCATCH.

When the exception is triggered the system continue the process after ENDTRY.

You must use ENDCATCH when you're using SYSTEM-EXCEPTIONS as the follow example:


DATA: result   TYPE i, 
          number TYPE i. 

CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 4 
                        OTHERS = 8. 
*  The value of number is 0 (  1 / 0 )
  result = 1 / number. 
  
ENDCATCH. 

* If sy-subrc is not equal 0 the SYSTEM-EXCEPTIONS was triggered
IF sy-subrc is NOT INITIAL. 
*  error 
ENDIF.

Regards,

Marcelo Ramos