Trapping error messages
-
When I
group = entities.add_face (my_face_points)
, andmy_face_points
are not coplanar, the application crashes. The API indicates that:if (group) puts "OK" else puts "Failed" end
But that didn't work for not coplanar. Is there any way to trap the error message, and continue running the application?
-
begin ### try to do something group = entities.add_face(my_face_points) result="OK." rescue ### what to do if it fails result="MISERABLE FAILURE!" end UI.messagebox(result)
-
@honoluludesktop said:
group = entities.add_face (my_face_points)
- why are you naming the variable
group
when you get a face in return? - avoid space between method name and brackets.
- why are you naming the variable
-
Thanks fellows, I will try to run with those tomorrow. Tom, perhaps I should have written
my_faces
in place ofgroup
. -
@honoluludesktop said:
Thanks fellows, I will try to run with those tomorrow. Tom, perhaps I should have written
my_faces
in place ofgroup
.It's much easier to follow the code when the variables aren't ambiguously named.
-
You are right, at the time it seemed OK, sorry:-(
-
TIG, Works great. I guess I now have to be careful not to bypass any errors that destroy the data base, and crash SU or the plugin:-)
I tried the following to aid debugging, but the failed command would not run after the Ruby panel opened.
begin group = @entities.add_group(@my_entities) rescue #comes here when @my_entities is not a "add_group" entity result = Sketchup.send_action "showRubyPanel;" group = @entities.add_group(@my_entities)#this did not run end
But I have lots of other uses for it, Thanks.
-
This is a fuller example using multi-rescues
begin # .. the process rescue SyntaxError, NameError => ex print "String doesn't compile; " + ex # .. tidy up rescue SystemCallError => ex print "System Call Error; " + ex # .. tidy up rescue StandardError => ex print "Error running script: " + ex # .. tidy up ensure # .. this is for a 'tidy up' when there's something 'wrong' # .. BUT it's NOT an 'error' per se # .. e.g. f.close unless f.nil? end
If you are using it for 'testing' code then just keep the Ruby Console open...
-
TIG, Thanks, is there a way to open SU with the Ruby Console open? Or, do you debug by keeping SU open, then cut and paste to the Web Console?
-
Add
Sketchup.send_action "showRubyPanel:"
into the start of your script just afterrequire 'sketchup.rb'
BUT remember to ### it out when you are done!
Advertisement