Using ABAP Exception classes with Message classes (Part 2 of 2)
In the previous post, we created a simple ABAP exception class that uses a Message Class. Now let’s see how we can use this class. It’s strongly recommended that you read part one before continuing.
The statement for raising (or throwing) an exception class that uses a Message class looks as follows:
raise exception type CX_DB_ERROR
exporting TEXTID = CX_DB_ERROR=>WRITE_ERROR
TABLENAME = ‘SPFLI’.
Let’s analyze this statement. First, we state the type of the exception, which is CX_DB_ERROR (that’s the exception that we created in the previous post). Next, we’re passing the parameters to the exception.
The first parameter answers the question – “What is the message we want to display?” It states the key of the text that we want to insert. Since we want to display a text for a “database write error”, we pass the key WRITE_ERROR (Remember it from last time? We created it on step 6).
The second parameter answers the question “What are the parameters needed for the message we want to display?” If you remember, our message had one parameter, the table name, so that we could imply that the write error occurred on this or that table. So here we pass the name of the table, in this example it’s the famous SPFLI.
So far we’ve looked on how to throw the exception. Now let’s take a look at the code for catching the exception.
data OREF type ref to CX_ROOT.data TEXT type STRING.
try.
... “Some method that throws CX_DB_ERROR
catch CX_DB_ERROR into OREF.
TEXT = OREF->GET_TEXT( ).
endtry.
In this simple code example, we catch the exception CX_DB_ROOT into a variable called OREF. We then extract the exception text into the variable TEXT by using the GET_TEXT() method.
At the end of this code execution, the variable TEXT should hold the following string:
“An error occurred when writing to table SPFLI”.
Note: There is another parameter which is commonly passed when creating an exception. It is called PREVIOUS, and it’s used to wrap exception in case of an exception chain. Those of you who have experience with Java or .NET are probably familiar with this concept. To all the others, don’t worry – we’ll mention exception chains later on.
5 comments:
Hi
I read this post two times.
I like it so much, please try to keep posting.
Let me introduce other material that may be good for our community.
SAP online training
Hi sir, to provide valuable information this very useful of SAP learners sap mm online training
After reading this post I got an idea about on this note.Really something grate in this article ,Thanks for sharing this. We are providing SAP courses training online. After reading this slightly am changed my way of introduction about my training to people. To know more Visit Us SAP PM Online Training Course
I appreciate you sharing this article. Really thank you! Much obliged.
This is one awesome blog article. Much thanks again.
sap online training
software online training
sap sd online training
hadoop online training
sap-crm-online-training
Nice Blog...
We offer SAP HR ONLINE TRAINING
Post a Comment