0
votes

Is there any way to supress all OLE related errors? For example if i try to access a property of server application that does not exist, there is runtime error "Error Accessing External Object Property" how to get rid of all the OLE related errors? I have used TRY...CATCH but i guess its not the way to solve this issue.

TRY
  lpn = Long(tab_1.tp_preview.ole_view.object.GetCurrentPageNumber)
  CATCH ( PBXRuntimeError re )
  CATCH ( OLERuntimeError OLERROR)
  CATCH ( RuntimeError RROR)  
FINALLY
  lpn = Long(tab_1.tp_preview.ole_view.object.GetCurrentPageNumber)
END TRY

GetCurrentPageNumber is not invalid property but it becomes invalid in my script because previous line access the ShowLastPage property.

There are few pages in the report and probably ShowLastPage do need sometime to reach the last page but next statement GetCurrentPageNumber is executed before that.

That is the reason i think i am getting the runtime error only for the first time when script runs. All subsequent executions of the same script are ok and no runtime error is shown because when the last page is already shown in the control, GetCurrentPageNumber does not show any runtime errors.

The solution to this problem i think is that i keep checking GetCurrentPageNumber in a loop until ShowLastPage finish its work. But the TRY...CATCH i wrote in my code cannot supress the runtime error message.

Please tell me how to do that.

I am using PowerBuilder 12.5 and Crystal Reports 11.5

2

2 Answers

0
votes

For your particular code >>> Your TRY-CATCH will only catch exception if the GetCurrentPageNumber is the culprit.

Anyway, I see no good reason to repeat your access to GetCurrentPageNumber in the FINALLY clause. This will always access the property twice. If the access within FINALLY throws an exception, that exception is not caught!

In case your call to the ShowLastPage method throws an exception, here is what I would consider (with "single responsibility" in mind):

  1. Wrap the call to ShowLastPage within a function (say, named of_ShowLastPage) that returns Boolean (true:succeeded, false:failed).

    Note: CATCH RuntimeError catches all unchecked exception types.

    FUNCTION boolean of_ShowLastPage( );
    TRY
       tab_1.tp_preview.ole_view.object.ShowLastPage
       RETURN TRUE
    CATCH (RuntimeError exRuntime)
       RETURN FALSE
    END TRY
    
  2. Use a separate function to wrap of_ShowLastPage in a loop waiting for success. Make sure you don't create an endless loop!

    FUNCTION boolean of_WaitForLastPage( )
    constant long maxRepeat = 1000
    boolean  displayingLastPage
    long     repeatCount
    DO
       displayingLastPage = of_ShowLastPage( )
       IF NOT displayingLastPage THEN
          repeatCount ++
          Yield( )
          Sleep(1)
       END IF
    LOOP UNTIL displayingLastPage OR (repeatCount >= maxRepeat)
    RETURN displayingLastPage
    
  3. Call this new function to ensure you show the last page when it becomes available. If only returns false it the last page couldn't be displayed within the 1000 seconds (defined by the constant maxRepeat)

NOTE: There are many ways to solve the same problem. Yield; Sleep may not be the best solution in your particular scenario.

0
votes

For your particular code >>> Your TRY-CATCH will only catch exception if the GetCurrentPageNumber is the culprit.

That is correct becuase ShowLastPage is never culprit and cause some delay when report control have to take its time to scroll to the last page. During that delay if GetCurrentPageNumber is called then GetCurrentPageNumber becomes the culprit otherwise it works.

Anyway, I see no good reason to repeat your access to GetCurrentPageNumber in the FINALLY clause. This will always access the property twice. If the access within FINALLY throws an exception, that exception is not caught!

Yes, that is correct, there is no need of GetCurrentPageNumber in FINALLY. Actually, i was trying different ways but knew FINALLY execute everytime.

In case your call to the ShowLastPage method throws an exception, here is what I would consider (with "single responsibility" in mind):

ShowLastPage method Does not throws exception but GetCurrentPageNumber does(as i mentioned in above case). And this is the only change i made in your suggested code. I changed code as following

Boolean _GetCurrentPageNumber()

Long currentPageNumbe = 0
TRY
  currentPageNumbe = Long(tab_1.tp_preview.ole_view.object.GetCurrentPageNumber)
  RETURN currentPageNumbe
CATCH (RuntimeError exRuntime)
  RETURN 0
END TRY

The 2nd method:

Long _waitForCurrentPageNumber()


Constant Integer maxRepeat = 1000
Long CurrentPageNumber = 0
Integer repeatCount

DO
  CurrentPageNumber = _GetCurrentPageNumber()
  IF NOT CurrentPageNumber > 0 THEN 
    repeatCount += 1
    Yield()
   Sleep(1)
  END IF
LOOP UNTIL CurrentPageNumber > 0 OR repeatCount >= maxRepeat
RETURN CurrentPageNumber

There are many ways to solve the same problem. Yield; Sleep may not be the best solution in your particular scenario.

Yes but it worked :) i checked, 1 to more than 42 thousands pages of the report i was testing, created no crashing problem. Thanks a lot for all of the suggestions.