Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to tell nested TRY CATCH level?
Message
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01034359
Message ID:
01034759
Views:
18
>>>>>>Hi all,
>>>>>>
>>>>>>Is there a functin like
TXNLEVEL()
that would report the nested level of TRY-CATCH block?
>>>>>>
>>>>>>TIA
>>>>>
>>>>>NO
>>>>
>>>>Graze, Fabio!
>>>
>>>Clearly it is possible to implement it with explicit code:
>>>
>>>
>>>* declare
>>>PUBLIC g_TRYLEVEL && or ADDPROP(_SCREEN.TRYLEVEL)
>>>
>>>g_TRYLEVEL = 0
>>>
>>>* on every TRY/ENDTRY
>>>
>>>TRY
>>> g_TRYLEVEL = m.g_TRYLEVEL +1
>>> .....
>>>
>>>FINALLY
>>> g_TRYLEVEL = m.g_TRYLEVEL -1
>>> .....
>>>ENDTRY
>>>
>>
>>Indeed, it's possible. I'm handling it a little differently now. In my methods, I made it so it returns a success flag, if fails I throw an exception.
>>e.g.
>>
>>define class myclass as custom
>>
>> funciton method2() as Logical
>>  local llSuccess as Logical
>>  local loE as Exception
>>  try
>>   &&do something....
>>   if not this.method1() ;
>>   then
>>    throw && this moves the execution pointer to catch block
>>   endif
>>   m.llSuccess = .t.
>>  catch to loE
>>   &&log the error and/or report to the user
>>   m.llSuccess = .f.
>>   exit
>>  finally
>>    if m.llSuccess ;
>>    then
>>      //do something
>>    else
>>      //do something else
>>    endif
>>  endtry
>>  return m.llSuccess
>> endfunc &&method2
>>
>> funciton method1() as Logical
>>  local llSuccess as Logical
>>  local loE as Exception
>>  try
>>   &&do something....
>>   m.llSuccess = .t.
>>  catch to loE
>>   &&log the error and/or report to the user
>>   m.llSuccess = .f.
>>   && throw && this does not escalate it to try-catch in method2 it loops
>>   exit
>>  finally
>>  endtry
>>  return m.llSuccess
>> endfunc &&method1
>>
>>enddefine
>>
>>I was hoping that when I throw exception from method1 it would be caught by TRY-CATCH in method2. However that's not the case, it loops in the same TRY-CATCH instead of escalating it to the higher level handler which is defined in method2.
>
>Catch escalation is done:
>
>SET TEXTMERGE noSHOW off
>CLEAR
>
>mc=CREATEOBJECT("myclass")
>rvalue=mc.method2()
>? rvalue
>
>define class myclass as custom
>
> function method2() as Logical
>  local llSuccess as Logical
>  local loE as Exception
>  try
>   &&do something....
>   llSuccess = this.method1()
>    ? "do something"
>  catch to loE
>   &&log the error and/or report to the user
>     ? "do something WHEN ERROR"
>     ?
>  finally
>  endtry
>  return m.llSuccess
> endfunc &&method2
>
> function method1() as Logical
>  local llSuccess as Logical
>  local loE as Exception
>  try
>   ERROR "CATCH THIS"
>   llSuccess = .t.
>  catch to loE
>  ? MESSAGE()
>
>   &&log the error and/or report to the user
>   throw && this does not escalate it to try-catch in method2 it loops
>  endtry
>  return m.llSuccess
> endfunc &&method1
>
>enddefine
>
Fabio,

Now I see what happened. If I call the error event from the catch block the exception is not caught by the outer level. The reason why I'm calling error event is that it has the error logging logic implement from previous versions, naturally I'd like to make as little change as possible. Try this out:
SET TEXTMERGE noSHOW off 
CLEAR
SET STEP ON 
mc=CREATEOBJECT("myclass")
rvalue=mc.method2()
? rvalue

define class myclass as custom

 function method2() as Logical
  local llSuccess as Logical
  local loE as Exception 
  try 
   &&do something....
   llSuccess = this.method1()
    ? "do something"
  catch to loE
   &&log the error and/or report to the user
     ? "do something WHEN ERROR"
     ?
  finally 
  endtry
  return m.llSuccess
 endfunc &&method2

 function error(errorno as Integer,method as String,lineno as Integer) as VOID 
 	messagebox("Hello, I'm your error!")
 endfunc && error
 
 function method1() as Logical
  local llSuccess as Logical
  local loE as Exception 
  try 
   ERROR "CATCH THIS"
   llSuccess = .t.
  catch to loE
  ? MESSAGE()
    this.Error(loE.ErrorNo,loE.Procedure,loE.LineNo)
   &&log the error and/or report to the user
   throw && this does not escalate it to try-catch in method2 it loops
  endtry
  return m.llSuccess
 endfunc &&method1

enddefine
Dawa Tsering


"Do not let any unwholesome talk come out of your mouths,
but only what is helpful for building others up according to their needs,
that it may benefit those who listen."

- Ephesians 4:29-30 NIV

Dare to Question -- Care to Answer

Time is like water in a sponge, as long as you are willing you can always squeeze some.

--Lu Xun, Father of Modern Chinese Literature

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform