0 votes
in Technology by (1.0m points)
Explain about SAP ABAP - Exception Handling?

1 Answer

0 votes
by (1.0m points)

An exception is a problem that arises during the execution of a program. When an exception occurs the normal flow of the program is disrupted and the program application terminates abnormally, which is not recommended, therefore these exceptions are to be handled.

Exceptions provide a way to transfer control from one part of a program to another. ABAP exception handling is built upon three keywords − RAISE, TRY, CATCH and CLEANUP. Assuming a block will raise an exception, a method catches an exception using a combination of the TRY and CATCH keywords. A TRY - CATCH block is placed around the code that might generate an exception. Following is the syntax for using TRY – CATCH −

TRY.      
Try Block <Code that raises an exception> 
  
CATCH  
Catch Block <exception handler M>  
. . . 
. . . 
. . . 
CATCH  
Catch Block <exception handler R>
   
CLEANUP. 
   Cleanup block <to restore consistent state>
 
ENDTRY.

RAISE − Exceptions are raised to indicate that some exceptional situation has occurred. Usually, an exception handler tries to repair the error or find an alternative solution.

TRY − 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. It is followed by one or more catch blocks.

CATCH − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The CATCH keyword indicates the catching of an exception.

CLEANUP − The statements of the CLEANUP block are executed whenever an exception occurs in a TRY block that is not caught by the handler of the same TRY - ENDTRY construct. Within the CLEANUP clause, the system can restore an object to a consistent state or release external resources. That is, cleanup work can be executed for the context of the TRY block.

Raising Exceptions

Exceptions can be raised at any point in a method, a function module, a subroutine, and so on. There are two ways an exception can be raised −

  • Exceptions raised by ABAP runtime system.

    For instance Y = 1 / 0. This will result in a run time error of type CX_SY_ZERODIVIDE.

  • Exceptions raised by programmer.

    Raise and create an exception object simultaneously. Raise an exception with an exception object that already exists in the first scenario. The syntax is: RAISE EXCEPTION exep.

Catching Exceptions

Handlers are used to catch exceptions.

Let’s take a look at a code snippet −

DATA: result TYPE P LENGTH 8 DECIMALS 2, 
exref TYPE REF TO CX_ROOT, 
msgtxt TYPE STRING. 
PARAMETERS: Num1 TYPE I, Num2 TYPE I. 
TRY. 
result = Num1 / Num2. 
CATCH CX_SY_ZERODIVIDE INTO exref. 
msgtxt = exrefGET_TEXT( ). 

CATCH CX_SY_CONVERSION_NO_NUMBER INTO exref. 
msgtxt = exrefGET_TEXT( ).

In the above code snippet, we are trying to divide Num1 by Num2 to get the result in a float type variable.

Two types of exceptions could be generated.

  • Number conversion error.

  • Divide by zero exception. Handlers catch CX_SY_CONVERSION_NO_NUMBER exception and also the CX_SY_ZERODIVIDE exception. Here the GET_TEXT( ) method of the exception class is used to get the description of the exception.

Attributes of Exceptions

Here are the five attributes and methods of exceptions −

S.No. Attribute & Description
1

Textid

Used to define different texts for exceptions and also affects the result of the method get_text.

2

Previous

This attribute can store the original exception that allows you to build a chain of exceptions.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Learn More with Blogmepost

blogmepost Online Test

NCERT Questions & Answers

  • NCERT Class 12 Maths Solutions
  • NCERT Class 12 Physics Solutions
  • NCERT Class 12 Chemistry Solutions
  • NCERT Class 12 Biology Solutions
  • NCERT Class 10 Science Solutions
  • NCERT Class 10 Maths Solutions
blogmepost Q&A

CBSE Questions & Answers Portal

  • CBSE Class 12 Maths Questions Answers
  • CBSE Class 12 Physics Questions Answers
  • CBSE Class 12 Chemistry Questions Answers
  • CBSE Class 12 Biology Questions Answers
  • CBSE Class 10 Science Questions Answers
  • CBSE Class 10 Maths Questions Answers
Blogmepost

Learn Math Formula, Revise Notes

  • JEE Crash Course Questions & Answers
  • NCERT Course Questions and Answers
  • CBSE Board Questions Bank
  • ICSE Board Questions Bank
  • Free Study Materials
  • Question & Answers Bank for All Classes
  • Text Book Solutions
  • Previous Year Question Paper
  • NCERT Book Solutions

  • NCERT
  • NCERT Solutions
  • NCERT Solutions for Class 12 Maths
  • NCERT Solutions for Class 12 Physics
  • NCERT Solutions for Class 12 Chemistry
  • NCERT Solutions for Class 12 Biology
  • NCERT Solutions for Class 11 Maths
  • NCERT Solutions for Class 11 Physics
  • NCERT Solutions for Class 11 Chemistry
  • NCERT Solutions for Class 11 Biology
  • NCERT Solutions for Class 10 Maths
  • NCERT Solutions for Class 10 Science
  • NCERT Solutions for Class 9 Maths
  • NCERT Solutions for Class 9 Science
  • CBSE Classes

  • CBSE Class 5 Questions
  • CBSE Class 6 Questions
  • CBSE Class 7 Questions
  • CBSE Class 8 Questions
  • CBSE Class 9 Questions
  • CBSE Class 10 Questions
  • CBSE Class 10 Maths Questions
  • CBSE Class 11 Commerce Questions
  • CBSE Class 11 Engineering Questions
  • CBSE Class 11 Medical Questions
  • CBSE Class 12 Commerce Questions
  • CBSE Class 12 Maths Questions
  • CBSE Class 12 Engineering Questions
  • CBSE Class 12 Medical Questions
  • BOARDS

  • CBSE
  • ICSE
  • NCERT
  • IGCSE
  • Andhra Pradesh
  • Bihar
  • Gujarat
  • Jharkhand
  • Karnataka
  • Kerala
  • Madhya Pradesh
  • Maharashtra
  • Punjab
  • Rajasthan
...