Saturday, February 2, 2013

SAP-ABAP-Exception-Handling-Classes-and-Usage

SAP-ABAP Exception Handling Classes and it's Usage in the language when writing ABAP Applications. In this post we are trying to give a brief overview on Exception Handling, at the run time how can we handle the exceptions are important in writing any applications. ABAPers refer this post for more details on Exception Handling. Click on read more to read this article.


SAP-ABAP-Exception-Handling-Classes-and-Usage 


SAP-ABAP Exception Handling Classes and it's Usage in the language when writing ABAP Applications. In this post we are trying to give a brief overview on Exception Handling, at the run time how can we handle the exceptions are important in writing any applications. ABAPers refer this post for more details on Exception Handling.


Exception Handling:

Exceptions are runtime anomalies or unusual conditions that a program may encounter while executing. Anomalies might include conditions such as division by zero, running out of memory. When a program encounters an exceptional condition, it is importing that it is identified and dealt with effectively.


Types of Exception Handling in ABAP

  • Exceptions in function modules

  • Class based exception handling.


Advantages of class based exception handling:

When an exception object is generated and an exception is raised or thrown, the exception serves two functions:

  • It changes the control flow.

  • It carries information about the details of the error.


Types of raising exceptions

·         Those raised by the runtime system, if a error is detected.

·         Those generated explicitly in the ABAP code.

Steps in raising an exception

  • Creation of exception object

  • Propagation along call chain until suitable handler is found
    (change of control flow)

  • If no handler is found: short dump


Exception class hierarchy:

            All the Exception classes inherit from  CX_ROOT.
      Static_check
For all the exception objects which are of type cx_static_check compiler checks whether it is caught or propagate to the calling method otherwise a compile time error is raised.


Dynamic check:


For exceptions of type cx_dynamic_check compiler doesn’t check whether it is handled or not. At runtime if exception a raised, if it is caught or propagated program execution continues otherwise short dump. 

If exceptions of this type are not handled in current method but handled in calling method then we need to explicitly use raising clause in the method signature of current method.


No check:

For exceptions of type cx_no_check runtime environment automatically propagates exception to the calling method.  

       
Issues while propagating exceptions:


For exceptions of type cx_static_check if not handled or propagated compiler raises error. For exceptions of type cx_dynamic_check if exceptions are not propagated then runtime environment gives short dump. 

For exceptions of type cx_no_check runtime environment automatically propagates exception to calling method.


Exceptions in method redefinition:

As we are not allowed to change the interface of the redefining method we are not allowed to use raising clause in the method interface. So we cannot propagate new exceptions other than those declared in the parent class method interface.


Try Endtry:


All the code between try and first catch statement are in protected area. We are allowed to use any no. of catch statements in a single try block. Try-endtry can be nested. For every try-endtry we can use only one cleanup block.


CLEANUP Block:


CLEANUP block can be defined in each TRY-ENDTRY structure. If the system has not found a handler for an exception, but the exception is handled in an enclosing TRY-ENDTRY structure or is propagated to a caller, the Cleanup block is executed before the TRY-ENDTRY structure is exited.



Handling Exceptions:


try.
                        catch cx_root.

                        catch cx_static_check.

endtry.


While handling exceptions catch blocks should be arranged from child to parent otherwise compile time error. Above code gives compile time error.



Attributes of Exception Class:


The attributes of exception classes are used to transport additional information on an error situation to the handler. The main piece of information is, however, always the fact that an exception of a particular class has occurred. The following attributes are inherited from 

CX_ROOT:

TEXTID


Used to specify the exception of a class more precisely by using several exception texts. Is evaluated in the GET_TEXT method.



PREVIOUS


If an exception is mapped to another exception, a reference to the original exception can be defined in this attribute via the EXPORTING addition of the RAISE EXCEPTION statement and by means of the IMPORTING parameter of the constructor with the same name. 


This can result in a chain of exception objects. In the event of a runtime error, the exception texts of all the exceptions in the chain are output. 

Mapping an exception to another exception is only beneficial if the context in which the original exception occurred is important for characterizing the error situation.


KERNEL_ERRID


The name of the associated runtime error is stored in this attribute if the exception was raised by the runtime environment.


Some Frequently used exception classes:


Stattic check:


·         CX_CLASS_NOT_EXISTENT.

·         CX_ABAP_DATFM_INVALID_DATE


Dynamic check:

·         CX_SY_MOVE_CAST_ERROR

·         CX_OS_CLASS_NOT_FOUND

·         CX_SY_TAB_RANGE_OUT_OF_BOUNDS

·         CL_ABAP_CLASSDESCR


No check:

·         CX_SHM_OUT_OT_MEMORY


Note Points to Remember:


  • Exceptions cannot be propagated by static constructors and event handler methods.

  • Exceptions whether they are static or dynamic occur at runtime only.

  • Cx_root, cx_static_check, cx_dynamic_check are abstract classes.

  • It is a good programming practice to declare exceptions in the method signature. This helps users of the method know what exceptions this method may raise.


Dear  ABAPers if you know any other points or interview questions related this exception handling  topic please drop a comment in the below comment box.

"You found the information helpful and want to say thanks? Your donation is enough to inspire us to do more. Thanks a bunch!"

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Categories

ABAP (1) ABAP Interview Questions (112) ABAP Open SQL Statements (1) ABAP Syntax Rules (6) ABAP WORKBENCH (2) ABAP-Interview-Questions (52) ALE IDOC (6) ALE IDOC Interview Questions (6) ale-idoc (6) ALE-IDOC-Interview-Questions (19) ALV Interview Questions (5) ALV-Interview-Questions (22) BADI (2) BAPI (1) BAPI Interview Questions (1) BAPI-Interview-Questions (14) BDC (6) BDC Interview Questions (6) BDC-Interview-Questions (9) big data (2) big data interview questions (1) Classical Reports Interview Question (3) Classical-Reports-Interview-Questions (22) Conditional Statements (1) Cross Applications (3) Cross-Applications (14) Data Dictionary (22) Data Type Questins (1) Data types (1) Data-Dictionary (48) Data-Type-Questins (6) Dialog programming (5) Dialog Programming Interview Questions (4) Dialog-Programming (30) DOMAIN Interview Questions (1) Domain-Interview-Questions (8) Function Module (2) hadoop (2) hadoop interview questions (2) hdfs (1) IDoc Tutorials (6) Interactive Report Interview Questions (4) Interactive-Reports-Interview-Questions (22) Internal Tables (1) interview questions (1) Lock Object Interview Questions (1) Lock-Objects-Interview-Questions (10) Logical Database (1) Modularization Interview Questions (4) Modularization-Interview-Questions (25) Module Pool Programming (5) Module-Pool-Programming (39) modules in sap (1) Object Oriented ABAP (19) Object Oriented ABAP Interview Questions (15) object-oriented-abap (2) Object-Oriented-ABAP-Interview-Questions (34) OOABAP (9) Reports (14) Reports Interview Questions (9) Reports-Interview-Questions (19) RFC (1) RFC Interview Questions (1) RFC-Interview-Questions (14) RICEF (1) RICEF Objects (1) SAP (4) SAP ABAP (4) SAP ABAP Interview Questions (42) SAP ABAP Introduction (46) SAP ABAP Message Types (2) SAP BADI Interview Questions (2) SAP Basics (71) SAP Books (2) SAP Certification (1) SAP CONSULTANTS (5) SAP CRM (1) SAP ENHANCEMENTS (3) SAP EXITS (2) SAP EXITS ( SAP ENHANCEMENTS ) Interview Questions (1) SAP Free Books (1) SAP HR (2) SAP Lock Object (1) sap modules (2) SAP Open SQL Statements (1) SAP R/3 Architecture (4) SAP Search help (1) SAP Smartforms (1) SAP Smartforms Interview Questions (2) SAP Tables (5) SAP Tcodes (10) SAP Views (1) SAP Webdynpro ABAP (12) SAP Work Processors (2) SAP Workflow (3) SAP-BADI-Interview-Questions (11) SAP-Enhancements (39) SAP-Exits (39) SAP-Exits-Enhancements-Interview Questions (3) SAP-HANA (1) SAP-HANA-Interview-Questions (1) SAP-Smartforms-Interview-Questions (2) SAP-Workflow (3) Scripts (3) Scripts Interview Questions (2) Scripts-Interview-Questions (32) Search Help Interview Questions (1) Search-Help-Interview-Questions (9) Smartforms (1) Table Maintenance Generator (1) Table-Maintenance-Generator (4) Tables in SAP (2) Tables Interview Questions (3) Tables-Interview-Questions (3) Type Group Interview Questions (1) Type-Group-Interview-Questions (7) Variable Declaration (1) Views Interview Questions (1) Views-Interview-Questions (5) Webdynpro (12) what is big data (1)

Protected Blog

 
This blog is not affiliated to SAP AG |SAP is trademark of SAP AG |The information collected from various sources use information with your own risk.