Notifications
Clear all

[Closed] error checking methods

ok ive got a rollout floating diolague and it all works fine except now i just want to do some error chacking so the user dosnt fall into any holes.

so i gather a simple method for this things is like

if classof Obj == editable_mesh then blah balah blah

this is fine except i was wondering if there where any other class comparisons you can do. eg can you ask it its geometry object in general even if its class is say a “sphere” or if a object is a helper.

i guess i would like to know what other methods people use? as i seem not to ba able to find much in the maxscript help on error checking. although i have seached through some example scripts and found afew small things.

any help would be apreciated

cheers

john

7 Replies
1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

This is not exactly “error checking”, this is called “idiot proofing”
There isn’t much about it in the help because there are so many ways a user can screw up that it is a whole art (and science) to prevent him from doing it and would require a whole separate help just for that…

As mentioned already in this thread, you can test at superclass level whether an object is of geometry superclass, helper superclass etc. But even these checks have some special cases. For example, when checking for SuperClassOf obj == GeometryClass, you have to also add AND classof obj != TargetObject because a TargetObject is a geometryClass object, but DOES NOT have a valid mesh and will crash most of your scripts. This problem has pre-MAXScript roots and you just have to know about it.

In addition to checking using classof() and superclassof(), you can also perform checks for properties – in other words, making sure an object has a specific property exposed. The two methods hasProperty() and isProperty() can be used to check whether a property is exposed or whether it is accessible.
For example,

if isProperty obj #radius do…

will make sure that obj has a property called .radius you could access…

Still, in a system as complex as Max, there are so many cases that you could miss it is not even funny. Did you know that a scene node might NOT have a .pos property exposed under some circumstances? For example, if you create a Daylight System controlled by the Solar_Time controller, that controller is a Transform Controller but does NOT expose a .position track, thus calling $.pos causes an error, but $.transform.row4 does not because ANY scene node has a transformation matrix, regardless of the controller that is assigned to manage it…

In cases you expect things to go wrong, you might use try()catch() to catch anything that you might have forgotten, but keep in mind that this could “mask” some genuine code errors you might have embedded in your script. So add the try()catch() as a last resort once you have made sure that your script is as bug-free as humanly possible…

Here are some examples for a sphere and a dummy:

 classof $
Sphere 
superclassof $
GeometryClass
 
 
classof $
Dummy
superclassof $
helper

Is that it ?!

cheers man yeah yeah thats it. i have seem people use Try and Catch. not really sure why this is beter over a if then stament

1 Reply
(@thatoneguy)
Joined: 11 months ago

Posts: 0

try()catch() can fail silently.

For instance instead of checking to see if an object exists before deleting it… You can just throw it in a try()catch() and if it exists it’ll get deleted… otherwise… you can carry on as normal.

I have some routines which are very very fragile and require dozens of conditions to be met before they can proceed. Instead of 30 if/then logic trees I can just put it all in a try() and if all the variables are defined properly it’ll do it’s thing. Other wise it’ll just log it, skip it and move on.

But like bobo said, definitely something you do as a last step. Otherwise you won’t necessarily see what’s going wrong.

Also really helpful because it’ll preview the execution before starting in on it. So if it does fail you can just throw up one error message and call it a day. With a dozen if/then statements it might start doing some prep operations which and then crash leaving open a ton of variables, transformations and what not all over the place.

wow some fast posting bobo. thanks man that clears a few things up. yeah idot proofing the main problem is that i’m the idiot that i’m proofing against :).

This give me a few options thanks again man fr your time.

cheers

john

thanks man,

been trying few try and catch now. like you said it beats all the if statments. and now i just have message boxes appear on any failer occurs. works nice

thanks again all

cheers

john

One good function to put in your catch() statement, perhaps inside a check for whether you want to debug, is getCurrentException(). It returns a string that contains the error that caused the try() to fail and go to the catch().

Alternatively, you can enable the maxscript debugger, go to its options, and disable the “Ignore caught exceptions” option. Any errors inside a try() will then still be printed, etc. This is a bit less user-friendly, of course.