forked from pynchmeister/LTV-Blockchain-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTryCatch.sol
More file actions
27 lines (22 loc) · 804 Bytes
/
Copy pathTryCatch.sol
File metadata and controls
27 lines (22 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// SPDX-License-Identifier:MIT
pragma solidity ^0.8.30;
interface Calculator {
function divide(uint a, uint b) external returns (uint256);
}
contract SafeMathCaller {
Calculator public calculator;
constructor(address _calculator) {
calculator = Calculator(_calculator);
}
function safeDivide(uint256 a, uint256 b) public returns (string memory, uint256) {
try calculator.divide(a,b) returns (uint256 result) {
return ("success", result);
} catch Error(string memory reason) {
return ("Revert with reason", 0); // e.g. "Cannot divide by zero"
}catch Panic(uint256 errorCode) {
return ("Panic occured", errorCode);
}catch (bytes memory) {
return ("Low level error", 0);
}
}
}