Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Understanding Try Catch
Message
From
12/07/2018 16:48:37
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01661136
Message ID:
01661149
Views:
60
>>>Hi,
>>>
>>>Do I understand correctly that if there is an error (typo or undeclared variable or whatever problem) within TRY/CATCH, the global ON ERROR DO MyErrorHandler() will catch it? That is, that TRY/CATCH is not "isolated" from the global error handling.
>>>Please let me know if I am correct. Or, if I don't understand it correctly.
>>>
>>>TIA.
>>
>>You understood it wrong. A Try/Catch isolates the error from outside block (silences the error if you do nothing in CATCH). You can make the error available to outside block by using THROW in catch. ie:
>>
>>
>>On Error Do errHandler
>>Try
>>	? "A"+15
>>Catch To oErr
>>	*	m.oErr.UserValue = "Error caught with catch."
>>	Throw
>>*	Throw "Error caught with catch."  && This is not the same thing
>>Endtry
>>? m.newVar
>>On Error
>>
>>Procedure errHandler
>>	? "Error: [["+Message()+"]]"
>>Endproc
>>
>>
>>PS: Personally I prefer using On Error. Try Catch is not as powerful IMHO, doesn't catch every error that ON ERROR catches for example.
>
>What about this code:
>
>ON ERROR do MyError
>
>TRY 
>IF adfasdfa
>ENDIF 
>ENDTRY 
>=MESSAGEBOX("End")
>RETURN 
>
>FUNCTION MyError
>MESSAGEBOX("Error happened")
>
>
>The MessageBox with error shows when IF with undeclared variable is used. Do you see what I am seeing?

No I see

"Error happened"

first and then "End".

As I explained above, TRY...ENDTRY silences the error (Variable ... not found). You don't have CATCH there to handle nor a THROW within to pass that to your ON ERROR.

Errors silenced by TRY ... ENDTRY are hardest to find IMHO. Instead of silencing handle them ( sometimes they may be silenced on purpose ).
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform