异常处理
在编写代码的时候通常会使用 try...catch
来捕获异常,例如:
import { ethers } from "ethers";
try {
const provider = new ethers.JsonRpcProvider(
"https://rpc.buildbear.io/outstanding-juggernaut-05cd9cc5"
);
// 提供了一个不存在的地址,会抛出异常
const balance = await provider.getBalance("0x");
console.log(balance);
} catch (error: any) {
console.log(error.code) // 错误代码
console.log(error.message) // 错误信息
}