メインコンテンツまでスキップ

36 Try and Catch

  • try / catch文は外部関数のエラーのみをキャッチすることができる。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

// try / catchを使うための外部コントラクト
contract Foo {
address public owner;

constructor(address _owner) {
require(_owner != address(0), "invalid address");
assert(_owner != 0x0000000000000000000000000000000000000001);
owner = _owner;
}

function myFunc(uint x) public pure returns (string memory) {
require(x != 0, "require failed");
return "my func was called";
}
}

contract Bar {
event Log(string message);
event LogBytes(bytes data);

Foo public foo;

constructor() {
// This Foo contract is used for example of try catch with external call
foo = new Foo(msg.sender);
}

// try / catch を使って外部コントラクトを呼び出す
// tryCatchExternalCall(0) => Log("external call failed")
// tryCatchExternalCall(1) => Log("my func was called")
function tryCatchExternalCall(uint _i) public {
try foo.myFunc(_i) returns (string memory result) {
emit Log(result);
} catch {
emit Log("external call failed");
}
}

// try / catch を使って外部コントラクトを作成
// tryCatchNewContract(0x0000000000000000000000000000000000000000) => Log("invalid address")
// tryCatchNewContract(0x0000000000000000000000000000000000000001) => LogBytes("")
// tryCatchNewContract(0x0000000000000000000000000000000000000002) => Log("Foo created")
function tryCatchNewContract(address _owner) public {
try new Foo(_owner) returns (Foo foo) {
// you can use variable foo here
emit Log("Foo created");
} catch Error(string memory reason) {
// catch failing revert() and require()
emit Log(reason);
} catch (bytes memory reason) {
// catch failing assert()
emit LogBytes(reason);
}
}
}