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

Try & Catch

Former Member
0 Likes
358

Hi,

i wont to now what this commend do and when its good to use it?

Regards

1 REPLY 1
Read only

Former Member
0 Likes
309

The occurrence of an exception is normally used to display an error situation. The handler of an exception must try to correct the error that has occurred, find an alternative solution, or (if this is not possible) at least bring the affected context into a consistent state and then forward the error. If a call hierarchy does not contain a handler for an exception, the program is ended with a runtime error. Since exceptions cannot be handled by means of program calls, all possible exceptions within the program itself must be handled to prevent a runtime error. This rule does not apply to the coding within procedures whose exceptions can easily be propagated to (external) callers.

Class-based exceptions are handled in the following control structure:

TRY.

... " TRY block (application coding)

CATCH cx_... cx_... ...

... " CATCH block (exception handler)

CATCH cx_... cx_... ...

... " CATCH block (exception handler)

...

CLEANUP.

... " CLEANUP block (cleanup context)

ENDTRY.

The TRY statement opens a control structure to be ended with ENDTRY, in which three statement blocks can be listed in a specified order (this is not obligatory).

1. A TRY block, in which exceptions can occur.

This exception block consists of all the statements between the TRY and the CATCH statement.

2. One or more CATCH blocks for catching exceptions.

These exception blocks are initiated with CATCH and ended with a further CATCH, CLEANUP, or ENDTRY.

3. A CLEANUP block for cleanup work after the exceptions have been caught.

This statement block is initiated by CLEANUP and ended with ENDTRY. A TRY-ENDTRY structure must not contain more than one CLEANUP block in precisely this position.

Like all ABAP control structures, TRY-ENDTRY structures can be nested in any statement blocks (see Controlling the Program Flow). In particular, the three statements blocks above can also contain complete TRY-ENDTRY blocks. Like all ABAP control structures, each TRY-ENDTRY structure must be contained fully in a processing block (event block, dialog module, procedure). This means that the application coding of a TRY-ENDTRY structure cannot include several processing blocks.

TRY Block

The TRY block contains the application coding whose exceptions are to be handled. This statement block is processed sequentially. It can contain further control structures and calls of procedures or other ABAP programs.

If an exception occurs in the TRY block or in a procedure called up here, the system starts by searching for a CATCH statement of the same TRY-ENDTRY structure. It then searches from the inside out for a CATCH statement in any enclosing TRY-ENDTRY structures that handle the event. The system may call this handler. If the system does not find a handler, but the TRY-ENDTRY structure is contained in a procedure, it tries to propagate the exception to the caller (see also Propagating Exceptions). Exceptions cannot be propagated in any processing blocks without a local data area (event blocks, dialog modules). A runtime error occurs immediately if the handler is missing.

If no exceptions occur in the TRY block, program execution is continued directly after ENDTRY after the block has been completed.

CATCH Block

A catch block contains the exception handler that is executed when a particular exception has occurred in the TRY block of the same TRY-ENDTRY structure. A TRY-ENDTRY structure can contain several exception handlers. The syntax for introducing an exception handler is:

CATCH cx_... cx_... INTO ref.

Any number of exception classes can be specified after CATCH. This defines an exception handler for all the specified exception classes and their subordinate classes.

After an exception occurs, the system searches through the listed exception handlers in the specified order. The first exception handler whose CATCH statement contains the corresponding exception class or one of its superclasses is executed. The system then continues program execution directly after ENDTRY. No subsequent exception handlers are considered. For this reason, the order of the different exception handlers within a TRY-ENDTRY structure must be based on the inheritance hierarchy of the specified exception classes.

The syntax check ensures that the handlers for more specific exceptions (subordinate classes) can only be listed before the handlers for more general exceptions (superclasses). For example, a handler for the most general exception class CX_ROOT can only ever be the last statement block before CLEANUP or ENTRY. Otherwise, none of the subsequent handlers would ever be reached.

With the INTO addition, a reference to the exception object can be placed a reference variable. This enables the attributes of the exception object to be accessed in the handler. The reference variable must be suitable for the exception. Its static type must be the exception class itself or one of its superclasses. You are advised to use an exception class below CX_ROOT or CX_ROOT itself rather than the general class OBJECT. Only these classes contain the methods relevant for exceptions.

Regards,

Santosh