Try-catch in Solidity
Solidity only had limited exception handling capabilities in the form of require
, assert
, and revert
functions prior to version 0.6.0. Solidity introduced the much-needed try-catch
functionality for implementing exception handling in contracts. The concept of try-catch
is quite simple. There is a try
block associated with a function call. This function should be an external function. If the called external function does not produce an error, the try
block is executed. Execution of code in the try
block means there have been no exceptions. Immediately following the try block is the catch
block and the code within the catch
block is executed in the case of revert
(exception) from the target function. The try
and catch
blocks execution are mutually exclusive. Either the try
block or catch
block will get executed.
The syntax of try-catch
is shown next:
Try <<function call>> returns (<<return values from function call>>...