How do you catch parameter type exceptions in SmartPy tests?

There's an example of exception testing in: https://smartpy.io/docs/scenarios/testing

An example from docs

    # Assert exception result
    # catch_exception returns an option:
    #      sp.none if the call succeeds
    #      sp.some(<exception>) if the call fails
    e = sp.catch_exception(c1.state(7), t = sp.TString)
    scenario.verify(e == sp.some("This is false: param > 5"))

I can't seem to get it to work with sp.nat type errors. I'm not sure what the t= value should be, if I'm even using it correctly at all.

import smartpy as sp

class Summarise(sp.Contract):
    def __init__(self):

    def sum(self, x):
        sp.verify((x >= 5) & (x <= 100), "Not in range 5–100")
        with sp.for_("i", sp.range(1, x + 1)) as i:
            self.data.storage += i

@sp.add_test(name="Summarise Test")
def test():
    scenario = sp.test_scenario()
    contract = Summarise()

    scenario += contract

    # Exception catch attempt
    sp.catch_exception(contract.sum(-1), t=sp.TString)

sp.add_compilation_target("summarise_compiled", Summarise())

I want a test to ensure the param type is nat and not int or anything else. This is why I'd like to explicitly check for the type exception error. To prevent someone from inadvertently changing the type later on. Perhaps there is another approach?

1 Answer 1


sp.catch_exception is used to test when you abort the contract execution with FAILWITH.

t is the expected type of the value at the top of the stack when you call sp.failwith(...), <>.open_some(...), etc...

As you could probably notice, SmartPy test interpreter already detects the type inconsistency and gives you a verbose error describing the problem. The call fails on the type-checker, the code doesn't even get executed.

  • Ah, now I can see how that works with the example in the docs. I would like a test that prevents someone screwing up the param type. So I would like to explicitly check for the type exception error, so that I know the param type is nat and not int. Is that not possible? Oct 24, 2021 at 22:16
  • 2
    That should be an end-to-end test. In SmartPy you can add sp.set_type(x, sp.TNat) below the entrypoint to enforce the parameter type to be a nat. In typed languages, you can't usually test the type of arguments without reflection. Oct 25, 2021 at 6:21
  • 1
    +1 to what Rodrigo said - it's good to get into the habit of doing sp.set_type(...) at the top of every entry point - makes the code easier to read/understand and also easier to debug
    – damian
    Oct 25, 2021 at 8:01
  • @RodrigoQuelhas You can check for the exceptions thrown when incorrect types are provided though. Just wondering if that's possible here. I'll certainly add the set_type snippet thanks for the tip! Oct 25, 2021 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.