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.

Read more...

Using ABAP Exception classes with Message classes (Part 1 of 2)

We’ve talked about Exception Classes, and we’ve talked about Message Classes. Now let’s see how we can combine these two together.

Let’s take a scenario, in which you have an exception that is thrown whenever a database error occurs in your program. Let’s call this exception class CX_DB_ERROR. We want our exception to display various messages that can also be translated later on to different languages. For example, “Error when writing to database” or “Error when reading from database”. So let’s combine ABAP Exception and Message classes.

1. First, create a message class through SE91. For this example’s sake, let’s call it CM_DATABASE_MESSAGES

2. Add a few messages to the message class. For our example, let’s add the message: “An error occurred when writing to table &1”. &1 indicates that a parameter would be inserted to the message. This parameter would be the name of the table that caused the error. We’ll get back to this in a few minutes.

3. Now open SE80 and create the exception class CX_DB_ERROR .

4. Remember that our message from step 2 states which table caused the error? Now’s the time to add this parameter. So click on the “Attributes” tab, and add an attribute for the table name. Let’s call it TABLE_NAME, and give it the type STRING.

5. Next, click on the “Texts” tab.

6. Enter a key to the message you would like to display on your exception class. In our example, let’s add the key WRITE_ERROR, indicating an error occurred when writing to the database.

7. Click on the “Message Text” button.

8. A new dialog opens, titled “Assign Attributes of an Exception Class to a Message”.

9. In the field “Message Class”, insert the name of the Message Class associated with your exception class. Let’s insert the class CM_DATABASE_MESSAGES, which we created earlier.

10. Now we need to choose the message number. Click on the choice button and choose the message “An error occurred when writing to table &1”. The number of the message should now appear in the field (as we created only one message in our example, it should be 000).

11. Now, you have to choose attributes that would be associated with the parameter &1. Click on the drop down list button, and choose the TABLE_NAME attribute we created earlier.

12. Save everything and activate.

You’ve just finished creating an exception class that uses a message class. In the next post, I’ll show you how to use the exception class with the message.

Read more...

Initializing an Internal Table

In case you have an internal table “itab”, and you wish to initialize it (clear it), here are some common statements:

CLEAR itab.
This command clears the header of a table, but keeps its content. The initial memory allocated for the table remains reserved.
(If you don’t remember what a header line is, you can find out here or here).

CLEAR itab[].
The content of the table is released, and the header line is not (kind of the opposite of the CLEAR command).

REFRESH itab.
Clears the content of the internal table completely. The initial memory allocated for the table remains reserved . The header content is also cleared.

FREE itab.
Initializes the internal table and releases its entire memory space, including the initial memory requirement. So using free deallocates memory.

For more information visit the SAP Help. Also, the following blog has nice examples of what happens to an internal table and its header by using each of the commands above.

Read more...

About This Blog

This blog contains tips, tricks, tutorial, and some of my personal experiences with the SAP ABAP language.

FEEDJIT Live Traffic Feed

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP