But I want this function to indicate to the main error if the position "pos" is not correct (higher than maximum value). Is it possible to return "void"? Perhaps I could return a pair (an error code + the object itself)? Which is the ideal solution for this kind of functions?
You cannot do this: returnvoid;, however you can return nothing via a function whose return type is void.
However, I'd recommend an integral value, that is zero when pos is in bounds, or a non-zero value that would indicate how many spaces out of bounds pos is.
EDIT: you could return a class that has the error code + the object itself.
Is your function getByPosition() going to be doing any more than what is shown? If not, just eliminate the function call entirely, and handle bounds checking prior to accessing the element in your array. The function call as provided is unnecessary.
Using throw is a potential solution, and not a bad one either. You'll just have to remember to catch it in your main. If you forget, it could lead to some bugz...
But throw isn't a bad option. Just make sure that your function is the only thing in the "try" block.