14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7

Exception Classes and Design by Contract

create an (optional) importing parameter with the same name as the attribute so

that every time the exception is raised the programmer can (optionally) pass on

that information to the instance of the exception class that is generated.

Later on, when you start processing instan ces of your exception class, you’ll be

very grateful that you have a mechanism to store such data and pass it onwards

into routines to analyze and respond to the error.

7.2.2 Declaring the Exception

For two of the three types of exception class (CX_STATIC_CHECK and CX_DYNAMIC_

CHECK), you have to specify the exception class in the signature of the method that

raises the exception. If you do have to specify the exce ption, then you do so in

Transaction SE24 by navigating to a method name in your class and clicking the

Exceptions button. The screen shown in Figure 7.4 appears.

Figure 7.4 Adding Exceptions to Global Class Methods

You just have to add one or more exception classes to the table in Figure 7.4; the

descriptions will fill themselves out.

As you can see in Figure 7.4, when you try to add a class-based exception to a

global method, amazingly, the Exception Classes checkbox is not selected by

default. Without that box selected, global methods can have exceptions that will

behave just like a function module, giving a list of exceptions that set the value of

SY-SUBRC. Luckily, the system is clever enough to recognize that a class starting

with ZCX is an exception class and will therefore select the Exception Classes box

for you.

If you’re going to declare the exception class in the signature of the method, then

the name of the exception class must make sense to the caller (i.e., it cannot be

266

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!