0

I'm doing an external call in a try/catch block. This external call provides me with return value that I'll use further down the line.

However, I'm getting a TypeError: Try can only be used with external function calls and contract creation calls.

How can I use the values returned by the external call while in a try/catch?

Here the line:

uint256 x;
uint256 y;

try
    (x, y) = IVault(_vault).liquidate()
{
   // Do something
} catch {
   // Do something else
}

2 Answers 2

3

Ok, I found the issue: it's possible to manage the return value by using the returns keyword:

try
    IVault(_vault).liquidate() returns (uint256 _x, uint256 y)
{
   // Do something
} catch {
   // Do something else
}
1
  • 1
    Mark as best response OP so others know it's solved.
    – VX3
    Commented Jan 6, 2023 at 21:41
0

You can use an ordinary if statement to check if the function call was successful

4
  • Hello. To improve the quality of your answer, you could provide an example of what yo uare proposing. Commented Jan 2, 2023 at 8:27
  • It's in the answer. If someone doesn't know how to do an if statement then all I can do is provide a code snippet, which the user has not asked for. In any case, he has found another way around it which differs from my approach.
    – VX3
    Commented Jan 6, 2023 at 20:09
  • SE has a simple model: Explanation + example. Commented Jan 6, 2023 at 20:13
  • No - SE has a model of giving a clear and concise answer. Examples are not mandatory if an explanation is sufficient. What more do you need to know about an if statement? If the OP needs further clarification they can ask it themselves.
    – VX3
    Commented Jan 6, 2023 at 20:35

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.