14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

7

Exception Classes and Design by Contract

there are more than one or two intermediate methods, CX_STATIC_CHECK is not

the ideal exception class to inherit from.

7.1.2 Dynamic Check (Local or Nearby Handling)

Exception classes that inherit from the CX_DYNAMIC_CHECK class are identical to

classes that inherit from CX_STATIC_CHECK in every respect except one. In the case

of CX_STATIC_CHECK, if you raise an exception wi thin a routine, then you must

either handle it locally or pass it up wards by mentioning the exception class in

the signature; if you don’t, then a syntax error occurs. In the case of CX_DYNAMIC_

CHECK, however, if you raise an exception within a routine, then you must either

handle it locally or pass it upwards by mentioning the exception class in the signature;

if you don’t, then no syntax error occurs. You get no sort of warning at all,

much less an error. However, if such an exception is raised at runtime and not

handled locally or passed on, then a short dump occurs.

This has confused a lot of people over the years. This type of exception class

seems to have no added benefit compared to CX_STATIC_CHECK but a really big

downside, in that you are likely to ge t a short dump without the syntax check

warning you well in advance. Why in the world would anyone ever use this type

of exception class?

The official explanation seems to be that you should use a dynamic check when

you can use preconditions to make sure yo u never get to the line that raises a

dynamic exception, meaning that the situation is so rare as to be deemed impossible.

This means that the programmer is supposed to try and ensure that the line

that raises the dynamic exception never gets reached, which makes raising such

an exception and then not dealing with it rather like an ASSERT statement.

In conclusion, if you do want to use CX_DYNAMIC_CHECK for whatever reason, then

the handling ideally has to be done in the method in which the exception is raised

(i.e., locally). You could pass it upwards by specifying the exception class in the

signature, but for an exception that’s never supposed to happen, that seems illogical,

Captain.

7.1.3 No Check (Remote Handling)

Exception classes that inherit from CX_NO_CHECK are the only ones that can leave

the routines in which they are raised without the exception class being explicitly

260

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

Saved successfully!

Ooh no, something went wrong!