Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why should i use Try...Catch...Finally ?

Status
Not open for further replies.

andreateh

Programmer
Jul 19, 2003
83
0
0
SG
I still not every clear about Try..Catch..Finally
Can i just put my error handling stuff in the object.error?

Like example below :

in oApp.error


Code:
LPARAMETERS nError, cMethod, nLine
DO CASE
   CASE nError = 1   &&In here i will try 
        DO xxxxx     &&to put all possible
   CASE nError = 2   &&error number here.
        DO yyyyy
   CASE nError = 3
        DO AAAAA
   OTHERWISE
        DO UUUUU
ENDDO
 
For starters, using TRY...CATCH is the only way you can trap errors in the report writer. I've also used TRY...CATCH when running server type of applications that you don't want to shut down when an error is encountered. The problem with the old technique is that it was hard to wrap a chunk of code in something that said, "If you run into any problems anywhere in this section, log the error and skip the rest of the section".


-BP (Barbara Peisch)
 
Thank for explaination. Where can i get some sample of using TRY..CATCH..FINALY that can show me "If you run into any problems anywhere in this section, log the error and skip the rest of the section". . Thanks.
 
Here's a simple example:

Code:
* Here's a black-box function, all it does is produce
*an error.
FUNCTION DoSomething( X, Y )
  * This error is forced, but is meant to
  *   illustrate that any unexpected error could
  *   happen in DoSomething:
  ERROR "Something Bad Happened while DoSomething"
ENDFUNC

FUNCTION AllocateCriticalResource
  PUBLIC gnCritical
  gnCritical = INT(SECONDS())
ENDFUNC
FUNCTION AllocateAnotherCriticalResource( tX )
  LOCAL lcLab
  lcLab = tran(gnCritical)
  PUBLIC PubVar_&lcLab
ENDFUNC

FUNCTION ReleaseCriticalResource
  RELEASE gnCritical
ENDFUNC
FUNCTION ReleaseAnotherCriticalResource
  LOCAL lcLab
  lcLab = tran(gnCritical)
  RELEASE PubVar_&lcLab
ENDFUNC
PROCEDURE Main
LOCAL llSuccess

  ?"Hi, Main is starting!"
  ?"About to try to DoSomething..."
  llSuccess = .F.
  TRY
    AllocateCriticalResource()
    AllocateAnotherCriticalResource()

    * We MUST be sure to call x.ReleaseCriticalResource
    * and Y.ReleaseCriticalResource before X and Y
    * go out of scope (and auto-call their .Destroy

    do DoSomething with 1,2

    llSuccess = .T.
  CATCH TO oExc
    StrToFile( oExc.Message+chr(13), 'ErrorLog.txt', .T. )

  FINALLY
    * Regardless of whether an error happens in DoSomething
    * This code still gets to execute.
    * This code would still execute even if we didn't have
    * a CATCH clause above.
    ReleaseAnotherCriticalResource()
    ReleaseCriticalResource()
  ENDTRY
  * Here, we can do more.  This code will execute only if
  *  there either is No error during the TRY above,
  *  or if any errors that happen ARE caught in a
  *  CATCH clause above.
  if llSuccess
    ?"The process Succeeded!"
  else
    ?"The process Failed!"
  endif

  ?"We can continue with our program like normal!"
  ?"Goodbye!"
ENDPROC

See here for more discussion:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top