There are two ways I know you can go about using exception handling to solve your question.
The first way is the use if simple if-statement:
for ( int i=0; i<max; ++i )
{
if (! (i < 0) || (i >= max) ) { cout << "Data: " arr1[1] << endl;
}
}
The second method is actually using Exception handling:
#include <iostream>
using namespace std;
#include <stdexcept>
int main()
{
int [] arr1 = {10,69,30,12,11,34,56,78,35};
for ( int i=0; i<max; ++i )
{
try{ cout << "Data: " arr1[1] << endl;
}
catch (out_of_range &ex) {
cout << e.what() << endl; //the error message...
}
}
return 0;
};
Sounds like Home work.
I don't like to help people earn your degree. They should earn by their efforts.
So show efforts first
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
The second method is actually using Exception handling:
#include <iostream>
using namespace std;
#include <stdexcept>
int main()
{
int [] arr1 = {10,69,30,12,11,34,56,78,35};
for ( int i=0; i<max; ++i )
{
try{ cout << "Data: " arr1[1] << endl;
}
catch (out_of_range &ex) {
cout << e.what() << endl; //the error message...
}
}
return 0;
};
This is more funny!
Good work for submitting to a professor teaching you Java ;)
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392