Write a function getValue that safely accesses elements of an array:
getValue
int getValue(const vector<int>& arr, int idx)
If idx is invalid (negative or outside array size), throw std::out_of_range with the message: Index out of range
std::out_of_range
Index out of range
The program already creates a vector, calls getValue, and catches exceptions. You only need to implement the getValue function.
Example Input:
5 10 20 30 40 50 2
Output:
Value: 30
Input:
3 7 8 9 5
Error: Index out of range1
Test Cases
Test Results
Input
Expected Output