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: 

TRY and ENDTRY

Former Member
0 Kudos

Hi,

What is the use of TRY and ENDTRY statement?

6 REPLIES 6

Former Member
0 Kudos

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.

Former Member

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.

former_member200338
Active Contributor
0 Kudos

Hi,

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

Regards,

Niyaz

rainer_hbenthal
Active Contributor
0 Kudos

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.

Former Member
0 Kudos

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.

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

Regards,

Sathish

former_member200338
Active Contributor
0 Kudos

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.