Skip to Content
avatar image
Former Member

TRY and ENDTRY

Hi,

What is the use of TRY and ENDTRY statement?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • avatar image
    Former Member
    Oct 11, 2007 at 10:02 AM

    Hi Hema,

    There are two options for raising class-based exceptions.

    1. System-driven raising in the runtime environment

    2. Program-driven raising in the ABAP program

    Syntax for TRY .. ENDTRY construct

    TRY.

    CALL METHOD o1->m1.

    PERFORM f1.

    CATCH cx_root. "Handler for all exceptions

    " ABAP code(What to do when error occures)........

    ENDTRY.

    FORM f1 RAISING cx_my.

    TRY.

    IF ...... RAISE EXCEPTION TYPE cx_my2. ENDIF.

    CALL METHOD o1->m3.

    CATCH cx_my1 cx_my3 INTO ex.

    RAISE EXCEPTION TYPE cx_my4.

    CATCH cx_my4.

    "Handler for exceptions of type cx_my4

    " ABAP code(What to do when error occures)........

    CLEANUP.

    "Cleanup section, used to restore to a consistant state

    " ABAP code........

    ENDTRY.

    ENDFORM.

    TRY" target="_blank">http://help.sap.com/saphelp_nw04s/helpdata/en/83/636d2012fc11d5991e00508b5d5211/frameset.htm">TRY and Endtry

    Regards,

    Sathish

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 11, 2007 at 10:03 AM

    Try this simple example.

    DATA: result TYPE i,

    number TYPE i.

    CATCH SYSTEM-EXCEPTIONS arithmetic_errors = 4

    OTHERS = 8.

    result = 1 / number.

    ENDCATCH.

    IF sy-subrc <> 0.

    write:/ 'Exception caught'.

    ENDIF.

    Reward points if usefull.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 11, 2007 at 09:59 AM

    Hi,

    Its mainly used for catch the Exceptions.

    TRY.

    [try_block]

    [CATCH cx_class1 cx_class2 ... [INTO oref].

    [catch_block]]

    ...

    [CLEANUP [INTO oref].

    [cleanup_block]]

    ENDTRY.

    Thanks.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 11, 2007 at 10:01 AM

    Basic form

    TRY.

    Effect

    You catch class-based exceptions in the statement block enclosed by the TRY and ENDTRY statements. To catch these exceptions, you define handlers within the specified TRY block. You introduce a handler using a CATCH statement, followed by the exception classes to be caught and the associated handler code. After the CATCH clauses, you can declare a CLEANUP clause. The statements in the latter clause will be executed if an exception is caught outsidethe TRY block. The structure of the TRY block is as follows:

    TRY.

    ... guarded section

    CATCH cx11 ... cx1n [INTO ex1].

    ... handlers for exceptions cx11 to cx1n

    CATCH cx21 ... cx2m [INTO ex2].

    ... handlers for exceptions cx21 bis cx2m

    ... other handlers

    CLEANUP.

    ... cleanup block

    ENDTRY.

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

    TRY statement is used when you expect some error/exception condition but not sure of which type of error may be present in a code block.

    in that case just wrap the code block in TRY ..ENDTRY block

    see this sample code.

    TRY.

    SORT T_ERGEB-MSG_T_SAP_ACTIVITY-ITEM DESCENDING.

    DELETE ADJACENT DUPLICATES FROM T_ERGEB-MSG_T_SAP_ACTIVITY-ITEM.

    CALL METHOD ZCO_SAP_ACTIVITY_TOMX=>EXECUTE_ASYNCHRONOUS

    EXPORTING

    OUTPUT = T_ERGEB

    CONTROLLER = L_CONTROLLER.

    COMMIT WORK.

    CATCH CX_AI_SYSTEM_FAULT INTO L_SYS_EXCEPTION.

    SY-MSGV1 = L_SYS_EXCEPTION->ERRORTEXT.

    SUBRC = 1.

    EXIT.

    ENDTRY.

    Here we are expecting that this code may generate a CX_AI_SYSTEM_FAULT type of exception. to handle this we have written a catch block.

    error of type CX_AI_SYSTEM_FAULT will be handled by this catch block

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

    Please check this thread

    https://forums.sdn.sap.com/click.jspa?searchID=93818&messageID=2288302

    The TRY block is split into different sections using the CATCH and CLEANUP clauses.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 11, 2007 at 10:01 AM

    Hi,

    For some cases, there might be possiblity of dump. hence you go for the statment with in try and endtry.

    Regards,

    Niyaz

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 11, 2007 at 10:02 AM

    Its another way of handling error.

    instead of

    do_something.

    if sy-subrc > 0.

    do_error_handling.

    endif.

    you can use

    try.

    do_something.

    catch <exception>

    do_error_handling.

    endtry.

    the advantage is that you can handle system error like division by zero or conversion errors without havening an abend.

    Add comment
    10|10000 characters needed characters exceeded