ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

on-exit opcode - can it see the return value?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • on-exit opcode - can it see the return value?

    So you can add an on-exit section to the end of a procedure, and it will execute code whenever the procedure ends, no matter how it ends, and optionally override the return value. Which is useful.

    My question is, is there a way that code running in on-exit can detect what the procedure's return value was?
    I know I could define a field in the procedure, set that to the return value, and then always return that field, and have on-exit reference that field. But I was hoping that on-exit would have some predefined access to the return value so I would not need to remember to always ensure the procedure does that.

  • #2
    You can use CEERTX/CEEUTX to call a procedure for anything other than a normal end of a procedure. You can pass a pointer to information you want the called procedure to have access to, so you could put the return value and any other info in a DS and pass a pointer to the DS. I haven't tried it but I suppose the return value could be altered. As for normal ends, that can be handled by coding standards. In my opinion there should be only one exit from a procedure.

    Comment


    • #3
      I suspect the answer at the present is "no, there's no way to see the return value". Your solution of putting the return value into a variable that you can check would be an easy solution. Of course, you could place a feature request with IBM, too.

      Comment


      • #4
        Originally posted by UserName10 View Post
        ... In my opinion there should be only one exit from a procedure.
        With ON-EXIT, arguably there _is_ only one exit from a procedure. (Assuming there aren't multiple RETURN statements in the ON-EXIT.)

        It's true that having early returns makes a procedure more complex, but sometimes all the additional code needed to avoid early returns can make the code even more complex.

        Comment


        • #5
          Barbara,

          It's true that having early returns makes a procedure more complex, but sometimes all the additional code needed to avoid early returns can make the code even more complex.
          It can be - but I submit it's still preferable to know the entry point and the exit point of a sub-procedure far more often than not. When it starts getting to the complexity you refer to it's time to evaluation if the task could be logically broken down into further sub-procedure(s) in order to simplify the code.

          Vectorspace,

          I submit that it's good coding practice to assign what you want to return into a variable anyway - and ultimately returning that variable. It'll clarify your code when debugging if you do that - and it addresses your question about on-exit.

          Comment


          • #6
            Originally posted by UserName10 View Post
            You can use CEERTX/CEEUTX to call a procedure for anything other than a normal end of a procedure. You can pass a pointer to information you want the called procedure to have access to, so you could put the return value and any other info in a DS and pass a pointer to the DS. I haven't tried it but I suppose the return value could be altered. As for normal ends, that can be handled by coding standards. In my opinion there should be only one exit from a procedure.
            There still is only one exit from a procedure. It's essentially a call to a subprocedure before leaving. It runs on an indicator you specify so you can code if it's a standard exit or an abnormal one. This is really a pretty cool feature. For example, you have some SQL Declare and subsequent OPEN statements - somewhere in the middle of the code it errors out and it bails to the ON-EXIT. If you have the SQL Close statements as part of the ON-EXIT they are always closed - whether the code ended normally or crashed half way through it doesn't matter. Then you can test to see if it was an abnormal end and act accordingly - maybe you want to do a rollback or notify the user of an error... but it's a graceful exit.

            Comment

            Working...
            X