sponsored links

Collapse

Announcement

Collapse
No announcement yet.

throws Exception in RPGLE

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

  • throws Exception in RPGLE

    Java programmer is asking to shows the similar implementation in RPGLE

    Code:
    public static void compute(int ten, int zero) throws MyException
    //going to divide ten by zero
    this is MyException class in RPGLE
    Source Name : CONDHDLR

    Code:
         H NOMAIN
          *
         d CondHdlr        PR
         d  Parm1                        12a
         d  Parm2                          *
         d  Parm3                        10I 0
         d  Parm4                        12a
          *
         pCondHdlr         B                   Export
          *
         D CondHdlr        PI
         d   InToken                           Like(CondToken)
         d   pErrInfo                      *
         d   Action                      10I 0
         d   OutToken                          Like(CondToken)
          *
         d  Error          s              1N   Based(pErrInfo)
          *
         d Resume          C                   10
         d PerColate       C                   20
          *
         d Log_Msg         S             40A
          *
         d InTokPoint      s               *
          *
         d CondToken       DS                  Based(InTokPoint)
         d   CondSev                      5I 0
         d   CondMsgNo                    2A
         d   Parm3                        1A
         d   CondPrefix                   3A
         d   Parm5                        4A
          /free
    
               InTokPoint = %Addr(InToken);
    
               Log_Msg = 'Starting Exception Handler';
               Dsply Log_Msg '*REQUESTER';
    
               //  Catching Exception goes here..
               //  X'1211' = Divide by Zero
               //
               Select;
               When CondPrefix ='MCH' and CondMsgNo = X'1211';
    
                  Log_Msg = 'Attempt made to divide by zero';
                  Dsply Log_Msg '*REQUESTER';
                  Error = *On;
                  Action = Resume;
    
               Other;
                  Action = Percolate;
               EndSl;
    
             Return;
          /end-free
         P                 E
    this is main Program which generate error but no more blowing...
    Source Name : TST004R

    Code:
          *
         d CEEHDLR         PR
         d  pCondHdlr                      *   PROCPTR
         d  CommArea                       *   CONST
         d  Feedback                     12a   OPTIONS(*OMIT)
    
         d  pConHdlr       s               *   PROCPTR
         d                                     INZ(%paddr('CONDHDLR'))
    
         d ErrorIND        s              1N   Inz(*Off)
         d Log_Msg         s             40A   Inz
          *
         d Num_Ten         S              5  0 Inz(10)
         d Num_Zero        S              5  0 Inz(00)
         d Answer          s              5  0 Inz
         d
          *
          /free
             *Inlr = *On;
    
               Log_Msg ='Starting Test PGM';
               Dsply Log_Msg '*REQUESTER';
    
               // Register the Condition Handler goes here...
               //
               CEEHDLR(pConHdlr : %ADDR(ErrorIND) : *OMIT);
    
               // Next line will generate an error and Condition Handler will
               // take care of it..
               //
               Answer = %Div(Num_Ten : Num_Zero);
    
               Log_Msg ='Ending Test PGM';
               Dsply Log_Msg '*REQUESTER';
    
             Return;
          /end-free
    compile it
    Code:
    CRTRPGMOD MODULE(QTEMP/CONDHDLR)
    CRTRPGMOD MODULE(QTEMP/TST004R)  
    CRTPGM PGM(TST004R) MODULE(QTEMP/TST004R QTEMP/CONDHDLR)
    call it

    CALL PGM(TST004R)

    check Message queue

    WRKMSG MSGQ(*wrkusr)

    Code:
    DSPLY  Ending Test PGM                  
    DSPLY  Attempt made to divide by zero   
    DSPLY  Starting Exception Handler       
    DSPLY  Starting Test PGM        
    Looks Like Java.! ha ?
    Read more..

    PS: anyhow I managed to Keep My Head Up !!!!!

  • #2
    Re: throws Exception in RPGLE

    Lets say you use this and have the divide by error. If that really and error or not? How will this be told to the business. Is the log going to be looked at?

    Why do I rant? A simple Monitor; on-error; will work and you can then even send out an alert(if really an error).

    It is neat what you did.
    Hunting down the future ms. Ex DeadManWalks. *certain restrictions apply

    Comment


    • #3
      Re: throws Exception in RPGLE

      A warning about using Action = Resume.

      You might expect it to resume at the next RPG statement. But actually it resumes at the next low-level statement in the generated code for the RPG statement. in your case, most likely it will resume just before it stores ... something, I don't know what, into Answer.

      And beware, if if you check what it stores into Answer and it seems ok, there's no guarantee that it will always behave that way.

      I think it would be much better to use MONITOR and ON-ERROR than to use a condition handler that will use Resume.

      I would even go further and say "Never use a condition handler that sets the action to Resume".

      Comment


      • #4
        Re: throws Exception in RPGLE

        Originally posted by DeadManWalks View Post
        Lets say you use this and have the divide by error. If that really and error or not? How will this be told to the business. Is the log going to be looked at?

        Why do I rant? A simple Monitor; on-error; will work and you can then even send out an alert(if really an error).

        It is neat what you did.
        sure, I never ever use such a technique for error handling and don't know in what business case we can apply this really.... but wanted to do a simple POC

        can't we use this technique generate the "SOAP fault" message of RPG-webservice ?
        even after validating all input of soap message and in case of error at the time of executing the business logic, if we can send back the soap fault to the client rather than soap exception....??

        but never did such & don't know really possible or not...

        glad if someone here can explain, for what they really introduce rpg handler
        Last edited by dhanuxp; April 5th, 2013, 11:24 AM.

        Comment


        • #5
          Re: throws Exception in RPGLE

          Originally posted by Barbara Morris View Post

          I would even go further and say "Never use a condition handler that sets the action to Resume".
          Thanks Barbara,
          Thanks for explanation....

          Comment


          • #6
            Good stuff and useful !!

            Comment


            • #7
              I wrote a 'Throw' function, if that's any use. The source is in file DLLUTILV20, though you'll need a couple of the other files too as they are /COPY files. Rather than a huge explanation of what's going on, it might be easier just to copy out the Throw procedure. You will need to provide your own message file. Note: References to RAYLIB are to my own work library, so substitute your own. https://github.com/TheCuckoo/ROOF

              Comment


              • #8
                If you don't deliberately "catch" the message in RPG, it will automatically propagate to the caller as long as the caller is in the same activation group. If you do deliberately catch the message (which you often have to do since RPG doesn't let you handle a specific error aside from RPG status code generating errors) then you have to re-send the error to the caller after catching it. The QMHRSNEM API works nicely for this.

                Comment

                sponsored links

                Collapse

                Working...
                X