Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
When to THROW exception?
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP1
Network:
Windows 2000 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01050675
Message ID:
01050690
Views:
7
As Frank has intimated, Throw is usually used for escalating an exception. I recently posted a message on something similar over on the Tek-Tips forum, so I'll repost the code here so you can see a simplistic example of how this works. Hopefully it gives a straight-forward look at the basic concept. Just cut-n-paste the code below into a PRG file and execute it.
DO testerr

PROCEDURE testerr
    TRY
        DO Whoops
    CATCH TO oExc
        oExc.COMMENT = [Perhaps Whoops caused this?]
        DO Oops WITH oExc, 0
    ENDTRY
    MODI FILE (ADDBS(SYS(2023)) + "TryCatchLog.txt") NOWAIT
ENDPROC


PROCEDURE Oops()
    LPARAMETERS toExc, tnLevel
    LOCAL lcErrorInfo, lcPadding
    lcPadding = REPLICATE(CHR(9), tnLevel)
    lcErrorInfo = lcPadding + [Exception Object ]  + TRANSFORM(tnLevel) + [:]+ CHR(13) + ;
        lcPadding + [  ErrorNo: ] + TRANSFORM(toExc.ERRORNO)  + CHR(13) + ;
        lcPadding + [  LineNo: ] + TRANSFORM(toExc.LINENO)   + CHR(13) + ;
        lcPadding + [  Message: ] + toExc.MESSAGE   + CHR(13) + ;
        lcPadding + [  Procedure: ] + toExc.PROCEDURE   + CHR(13) + ;
        lcPadding + [  Details: ] + toExc.DETAILS   + CHR(13) + ;
        lcPadding + [  StackLevel: ] + TRANSFORM(toExc.STACKLEVEL)   + CHR(13) + ;
        lcPadding + [  LineContents: ] + toExc.LINECONTENTS   + CHR(13) + ;
        lcPadding + [  Comments: ] + toExc.COMMENT   + CHR(13) + CHR(13)
    =STRTOFILE(lcErrorInfo, ADDBS(SYS(2023)) + "TryCatchLog.txt", 1)
    IF TYPE("toExc.UserValue") = "O"
        DO Oops WITH toExc.USERVALUE, tnLevel + 1
    ENDIF
ENDPROC

PROCEDURE Whoops
    TRY
        DO Sub2
    CATCH TO oExc1
        oExc1.COMMENT = [Perhaps sub2 caused this?]
        THROW oExc1
    ENDTRY
ENDPROC

PROCEDURE Sub2
    TRY
        DO sub3
    CATCH TO oExc2
        oExc2.COMMENT = [Perhaps sub3 caused this?]
        THROW oExc2
    ENDTRY
ENDPROC

PROCEDURE sub3
    TRY
        IF 6=MESSAGEBOX('Would you like to throw an error now?',4)
            ERROR 10  && throws syntax error
        ENDIF
    CATCH TO oExc3
        oExc3.COMMENT = [The user decided to throw an exception.]
        THROW oExc3
    ENDTRY
ENDPROC
>Hi,
>I am new to use TRY...CATCH...ENDTRY statement to handle error.
>In my practise, I always store loexception.message to my object.cErrorMsg if any exception catched and passed .T./.F. to calling program.
>Can anyone give me some ideas, when should I THROW the exception instead of storing error msg tro property and return boolean to calling prg?
>
>Thank you
Previous
Reply
Map
View

Click here to load this message in the networking platform