This website is not affiliated with, sponsored by, or approved by SAP AG.

ABAP OO - Exit instance from method

Development (ABAP Development WorkBench, ABAP/4 programming)

Moderators: Snowy, thx4allthefish, YuriT, Gothmog

ABAP OO - Exit instance from method

Postby Eggy » Fri Apr 17, 2015 8:41 am

I've written a class that handles functionality around files on disk. During the processing of a file, I write log records to an instance table in the object. When the log record contains an error, I would like to stop processing for this object. Problem I encounter here is that I can raise exceptions or events, but eventually processing resumes from where I left off.

Code: Select all
report
- constructor
- - create_record
- - - open_file
- - - - add_log

For example when there was an error opening the file, an error message will be written to the log in method add_log. From there I would like to completely exit the object and return to the report. When I raise an event to, for example, write log to file and close any datasets, afterwards processing returns to the add_log method and then continues back to open_file like nothing happened.

When raising an exception (non class-based), that exception has to be caught directly after the method. That means I would have to rewrite all the methods to become one big clusterf*ck of TRY/ENTRY blocks to be able to catch all possible exceptions.

Would I have to create an exception class and introduce a TRY/ENDTRY coding block in de report to be able to exit right from the add_log method?
Eggy
 
Posts: 309
Joined: Wed Jan 11, 2006 8:50 am

Re: ABAP OO - Exit instance from method

Postby Gothmog » Fri Apr 17, 2015 10:22 am

You can use the RAISING addition to the METHODS declaration to propagate the exceptions to the caller, you don't have to catch it directly.
However, you can't do that for event handler methods - any exception in an event handler has to be handled locally.
68 74 74 70 3a 2f 2f 74 69 6e 79 75 72 6c 2e 63 6f 6d 2f 62 64 6f 37 6d 77 67
Gothmog
 
Posts: 1941
Joined: Wed Sep 12, 2007 4:46 am
Location: Probably not home

Re: ABAP OO - Exit instance from method

Postby Eggy » Mon Apr 20, 2015 2:17 am

Thanks for the quick reply! I've been playing around with exceptions, but keep ending up in a shortdump. As a quick test, I've created a class with three methods (1, 2 and 3). 1 calls 2, 2 calls 3 and 3 will raise an exception. For method 3, I've entered ERROR as an exception in the correct tab. From within the method, I use 'RAISE error'.

In the report that uses the class, I create the instance and call method1. This works up until the RAISE statement... that ends up in a shortdump:
"Since the exception was not caught by a program higher up in the call hierarchy, processing was terminated."

I'm expecting this will not work since it's the 'old way' of raising exceptions, and I will need to introduce an exception class. I've done so and set my exception class ZCX_EGGY for method3. From within method3, I try to 'RAISE EXCEPTION TYPE zcx_eggy', but then I get the message I can't generate an instance of the class from anywhere outside of the class.

This has got to be a very simple and basic step I'm missing...

Update
And indeed it was a very basic step... the exception class had been created as PRIVATE, making it impossible to raise an exception like this. Changing that to PUBLIC (and FINAL) solved the issue.
Eggy
 
Posts: 309
Joined: Wed Jan 11, 2006 8:50 am

Re: ABAP OO - Exit instance from method

Postby Eggy » Wed Apr 22, 2015 3:10 am

Still not completely working as I would expect, can you help me out one more time? :)

I've introduced an exception class (sub of cx_static_check), that can be used. In my add_log method I check whether the message was en error message and if so, RAISE EXCEPTION TYPE zcx_exception.

Currently, that ends up in the CX_ROOT catch block in the calling program, telling me "An exception (CX_SY_NO_HANDLER) occurred". Right above the CATCH cx_root, I have the CATCH zcx_exception, but it's not getting there.

The add_log method has the RAISING zcx_exception addition. In order to be able to handle the exception in the calling program, will I have to add the RAISING zcx_exception to all methods in the call stack? That would mean all methods that call add_log (which are quite a few) will have to be able to raise the same exception, and all the other in the stack as well. I can't imagine that's the way it's supposed to work.

I was hoping to raise the exception in add_log, and then immediately end up in the corresponding catch block in the calling program.
Eggy
 
Posts: 309
Joined: Wed Jan 11, 2006 8:50 am

Re: ABAP OO - Exit instance from method

Postby Gothmog » Wed Apr 22, 2015 6:55 am

In order to be handled, the exceptions has to be propagated at all levels of the call stack.
Here, you are catching the exception raise by not propagating your exception - hence the fact that it is a CX_SY_NO_HANDLER exception, and that it is caught in the CATCH cx_root block.
68 74 74 70 3a 2f 2f 74 69 6e 79 75 72 6c 2e 63 6f 6d 2f 62 64 6f 37 6d 77 67
Gothmog
 
Posts: 1941
Joined: Wed Sep 12, 2007 4:46 am
Location: Probably not home


Return to ABAP

Who is online

Users browsing this forum: No registered users and 13 guests





loading...


This website is not affiliated with, sponsored by, or approved by SAP AG.