How can I use a local exception class in a global class? (just kidding, it’s not a #notablog 🙂 )
Couple of weeks ago I was developing a BADI and came with the need of throwing an exception from one of my private methods to the public method so I would proceed with the processing or not. After some brief internal discussion with other developers whether would be better to A) create an exception class for this BADI implementation or b) create a generic BADI exception class, Ben Patterson came with the idea of a private exception class for this BADI. Good idea!
So, in my BADI implementation class I went to the menu “Go to->Local Definitions/Implementation->Local Definitions/Implementations and create a definition for my class:
CLASS lcx_my_exception DEFINITION INHERITING FROM cx_dynamic_check. ENDCLASS. "lcx_my_exception DEFINITION
Then I activated the code, no errors, went to my private method definition and on the exceptions, typed lcx_exception (with the exception classes checkbox checked) and…
The problem (a bug?)
What?!?!? It didn’t look good, so after some failed attempts to solve it by myself, I came to SCN and among other stuff I found this discussion by Dirk Wittenberg:Local exception class in global class
Well, seems there’s no solution… I came back to talk to the team and Ben suggested me to declare the superclass in the signature, and raise my local exception in the implementation (quite smart this Ben Patterson guy). This was my reply to Dirk in the discussion above. It was a workaround that did the trick, but Dirk was not really happy, nor was I.
I tried a bit more stuff and could not believe it was not possible. If you can declare your local types and use them in methods signatures, why not local exception classes?
Last night I was playing on my trial system at home when I saw the light
Ok, it wasn’t like this and it was night so the lights were already on, but I thought “what if I try to add this exception in the source-code based mode?”.
So to the code based I switched, scrolled down to my private method definition and typed:
METHODS change_status IMPORTING !iv_object_number TYPE j_objnr !iv_status TYPE j_status RAISING lcx_exception .
Activate the code, no errors. Went back to form based, and I now can see my local exception declared in the signature:
Instead of going to the source code based mode you can also go to your private/protected/public section, it works the same way. Important thing is: form based = bad, source code based = good. I reckon there would be no problem if using ADT/AiE
The End 😀
PS1: This is my very first technical blog. No rocket science, no HANA/Cloud/Mobile stuff, but I hope it’s useful to someone.
PS2: I was not going to write a blog, rather just reply to Dirk’s thread. But for some reason I cannot reply to it, so blog it is.