1

I made the below line of code to throw a error on purpose for a tutorial I am doing. I think I understand why this would throw an error but just to be sure can someone explain to me why it would throw an error?

contract WillThrow {
    function aFunction() public pure {
        require(false, "Error message");
    }
}
1

1 Answer 1

1

You pass a boolean expression to the require function like require(booleanExpression, stringErrorMessageIfTheExpressionTurnsOutFalse);

So, if you pass a hardcoded true like this: require(true, "This will never throw an error"), then it will never throw an error.

If you pass a hardcoded false like this: require(false, "This will always throw an error"), then this will always throw an error.

If you pass an expression like require(msg.value > 0, "The value should not be zero"), then it will only throw an error if the value is 0, because we are requiring the value to be greater than 0, and if it's 0 the boolean expression will return false, and it would throw an error and show the error message pass as the second parameter to the require function.

So, in your example, it will always throw an error because you hardcoded false.

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.