wap to throw and handle array out of bound exception
#include<iostream>
using namespace std;
int main()
{
const int N=5;
int arr[N] = { 10,20,30,40,50 };
int ind;
do
{
cout << "\nPlease, enter an index of array (-1 - exit program): ";
cin >> ind;
if (ind == -1)break;
try
{
if (ind >=sizeof(arr)/sizeof(arr[0]))
throw "Out of bound exception!";
cout << "Result of indexing is " << arr[ind];
}
catch (const char* exception)
{
cerr << "Error: " << exception << endl;
}
}while (true);
}
Comments
Leave a comment