I have a small GUI application with a text-field and a button. The button triggers a function which tries to read a number from text-field. An exception is raised if the text-field is blank or has non-numeric text.
I am trying to catch error if text-field does not have a value or has text value rather than a valid number:
calc: does [
try [x: to integer! num_field/text]
catch [ print "Could not get number"]
print "Number read"
]
Following also does not work:
calc: does [
try [x: to integer! num_field/text]
throw 123
print "Number read"
]
catch 123 [ print "Could not get number"]
I am not sure how to use try, throw and catch here. I tried to check section 10 of http://static.red-lang.org/red-system-specs.html but could not really understand.
How can this be solved? Thanks for your help.
TRY
will still pass an error if one is generated, however it isn't triggered unless it's the last value evaluated.You could use the following:
Aside from
TRY
, there is alsoATTEMPT
which will just returnNONE
if an error occurs. UnlikeTRY
, you can't analyze the error object.CATCH
andTHROW
are more flow control functions in Rebol/Red than error handlers, they break out of however many stack levels they cross: