There’s no way catch hitting the instruction limit, but there are a few other options:
-
Deterministic time slicing (DTS) basically automatically does what you want - it’ll pause your canister when it hits the limit and resume at the same point next time your canister is scheduled.
-
You can break a large function up into smaller pieces a chain them together by having your canister call itself between each chunk. Then each chunk needs to finish within the instruction limit, but the total work can be much larger.
-
If you need to dynamically trigger the self-call, you can use the performance counter to check when you might be getting close to the instruction limit.