Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Does Return Statement Exit from features as well as functions?
Jonathan_Hutchinson
Member Posts: 91 PRO
Take the below example in a feature body:
if (definition.inputMethod == InputType.DIAMETER_VALUE)
{
setVariable(context, definition.variableName, rounded);
return;
}
Should this not execute any further code within the feature? I am currently seeing that it doesn't, at least not without an else block. Which I guess makes sense, but I thought the return statement would override anything else and run no further lines.
Another interesting thing I noticed is that, it seems, things done above a throw aren't carried out if a throw is encountered. So in the below example, on the condition of something, change the value of a variable, and also provide an informative throw. But that seemed to negate the action of setting the variable. Is that expected behaviour? Or is throw not the right thing to call in this sort of case?
Should this not execute any further code within the feature? I am currently seeing that it doesn't, at least not without an else block. Which I guess makes sense, but I thought the return statement would override anything else and run no further lines.
Another interesting thing I noticed is that, it seems, things done above a throw aren't carried out if a throw is encountered. So in the below example, on the condition of something, change the value of a variable, and also provide an informative throw. But that seemed to negate the action of setting the variable. Is that expected behaviour? Or is throw not the right thing to call in this sort of case?
if (!isundefinedoremptystring(something))
{
setVariable(context, definition.variableName, rounded);
throw "message for the throw";
}
0
Comments
Throw is meant for exceptions. If you want all the code to run, and then stop and show a message, you can use reportFeatureWarning or reportFeatureMessage.
Return; isn't obvious in featurescript because you don't need to declare function return types. Void isn't a thing.