Answer to Question #273584 in C++ for xyz

Question #273584

wap to throw and handle array out of bound exception

1
Expert's answer
2021-11-30T18:41:48-0500
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS