Using or not using ATTEMPT
15-Mar-2007 14:07 Filed in: REBOL
Beginners
ATTEMPT is a really nice function. It makes it simple
for you to skip errors in code if a part of your code
fails. Such as:
This is good if you don't really care about a math error, it's pretty obvious what goes wrong and just want to keep going.
I use this combination often:
As it's simple enough to debug by PROBE'ing a or b.
Therefore if you are building a larger script for a customer, and don't want the console output pop up suddenly in his/her face with strange error outputs, it can be quite tempting to just say:
ATTEMPT will not only keep your code running now, it will also completely hide otherwise obvious errors. If you use it like this, you lose of control over your code. Your application will not only not crash, by not responding to a user action, but you won't know at all what goes wrong.
Never use two nested ATTEMPTS to print the number of candybars per child:
Any number of things could be wrong in even this small bit of code:
It really is much better to do proper handling of errors than just to ignore them in the long run, It leads to unpredictable behavior and frustrating all-night debugging sessions.
>> a: 2
>> b: 0
>> attempt [a / b]
>> none ; We're fine. Keep moving.
This is good if you don't really care about a math error, it's pretty obvious what goes wrong and just want to keep going.
I use this combination often:
any [attempt [a / b] "Math Error"]
As it's simple enough to debug by PROBE'ing a or b.
Therefore if you are building a larger script for a customer, and don't want the console output pop up suddenly in his/her face with strange error outputs, it can be quite tempting to just say:
attempt [
huge amount of code, perhaps an entire application
]
ATTEMPT will not only keep your code running now, it will also completely hide otherwise obvious errors. If you use it like this, you lose of control over your code. Your application will not only not crash, by not responding to a user action, but you won't know at all what goes wrong.
Never use two nested ATTEMPTS to print the number of candybars per child:
unit: " candybars per child"
print any [
attempt [
join any [
attempt [candybars / children] default-value
]
unit
]
"Uhmm.. error!"
]
Any number of things could be wrong in even this small bit of code:
- default-value might not be set or is the wrong type
- candybars and/or children might not be set or are the wrong type
- children might be 0.
- Heck JOIN may even have been redefined by accident in a different section of your program! You won't notice...
It really is much better to do proper handling of errors than just to ignore them in the long run, It leads to unpredictable behavior and frustrating all-night debugging sessions.
|