Check bounds before using index.