I'm new to solidity and smart contract programming. I'm currently using the online remix IDE to run my program. I want to display a error message when the user tries to ask for the value of an array element that does not exist.

This is my array

offer[] public asks;

If my understanding is correct the above line automatically generates a getter function, the user inputs the array index value 'i' and the function returns asks[i], how do I prompt a error message saying 'array index exceeded' when the user inputs invalid index value?

1 Answer 1


Here is a simple example.

You probably want to use mapping for this.

contract Validator {

// record of valid ids  
mapping(uint => bool) isValidId;  // make it public for a getter

// function to return valid for understanding only since we have a getter.

// this is the same ad calling isValidId(id) if public
function isvalid(uint256 id) public returns(bool) {

    return isValidId[id];
  // set the state of an id to true
  function setValid(uint256 id) public onlyOwner {

    isValidId[id] = true;

function thatRequireValidId(uint256 id){
 require(isValidId[id], "Not a valid ID");

  • Is there a way to do this using arrays? I would like to know the length of the array at a later part of my program. Oct 27, 2021 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.