Try catch block


#1

Hello!

Your Skookum is really awesome.

I have a one question. Is there any try catch analog in syntax? How to avoid, for example, division by zero?

Thanks.


#2

There isn’t try/catch right now, I’m sure the :sk: folks can provide more details about that. You need to do your checks by hand like in other languages without try/catch. So make sure your denominator isn’t 0 before dividing.


#3

Thanks for the praise @AlexanderKai!

As @error454 says, SkookumScript does not use try/catch. It was decided that as a scripting language to keep it simple and to focus it on high level logic and then rely on the underlying C++ engine for low level errors such as divide by zero.

Since it is expected that SkookumScript will be used to make games used by the public rather than tools for example (though you can use :sk: for tools), you want to test for and eliminate any such potential errors rather than program around them.

That being said, for many fail conditions at the SkookumScript level it can be nice to register closures to handle them. Likewise if one coroutine completes because of a fail condition and it is in a race expression then it will go to the next line where recovery code can be run and then potentially loop back to regular game code again. There are also a few commands in the Debug class that you might want to check out.

If you have a strong case for additional error handling or debugging or any other cool ideas we will be happy to add it to SkookumScript. :madsci:


#4

Can you give an example of this?


#5

treat it like an event system that notifies you when something has failed, the closure can capture information about the failure, essentially a failure object

or change the flow of the code based on redefinition of closures that are contained in variables…

or something else =), there is a lot you can do with closures.


#6

I’m emphatically nodding to everything you’re saying but with a mild deer caught in the headlights look on my face :deer::oncoming_automobile:

I was just scratching my head for a real world example that was both useful and low-key enough to be understandable by everyone on a project. Here is what I came up with as an example of a failure handler for a fictitious division operation that would return the answer to all things.

!failure_handler : (Real n Real d)[println("Had an oops, hard coding to 42: " n " " d) 42]

!divider : (Real numerator Real denominator (Real n Real d) codez)
[
  !result : 0.0
  if denominator = 0.0
  [
    result : codez(numerator, denominator)
  ]
  else
  [
    result := numerator / denominator
  ]
  result
]

divider(2, 4, failure_handler ) // returns 0.5
divider(2, 0, failure_handler ) // returns 42

I’d love to see examples that exist in actual projects.