If there are no objects, the debugger will pop a warning message and return 0. It is not the most elegant way and potentially annoying if you have a large project that you are debugging regularly.
I'd suggest something like:
set 'Number' to CountMatching("Sprite")
It won't return the error. It is SOL sensitive, as well, so you can put it within context.