For the next feature in our Road to Neo3 series, we’ll be looking at the new exception handling mechanism under development for Neo3’s virtual machine (VM). The addition of exception handling capabilities to Neo smart contracts is intended to make them more robust, behaving more like traditional applications that can recover from unexpected errors.The Neo Virtual Machine
All contracts deployed to the Neo blockchain are executed within NeoVM by nodes on the network. When a contract is invoked, the inputs associated with the invocation transaction are run through the contract bytecode inside the VM. An interactive guide to how the NeoVM works can be found here.
Every node on Neo will execute the same code with the same VM, so the result is deterministic whether the result of the invocation succeeds or fails. This is good because it means that if the transaction had invalid inputs (like trying to send more assets than an address owns), it won’t go any further and the contract execution will stop.
In some cases, it may be desirable for a contract to be able to handle unexpected situations gracefully and continue their operation. To make this possible, developers need to be able to equip their contracts with the ability to predict such events and react to them with case-specific logic.
This is achieved by implementing exception handling mechanisms, which allow developers to write special instructions to be used in the event of an unexpected error. These could simply log and notify the developer of the particular issue encountered to help with debugging, or they could provide further code for analyzing and processing the invocation.Initial Try-Catch proposal
The common approach to handling exceptions is a try-catch mechanism, which enables a program to test for exceptions ahead of time rather than randomly encountering one and crashing. The program will run the code to be examined in th...