hello everyone,
can anyone please help me to solve the following problem.

Write a program

  1. To demonstrate division operation handling the arithmetic Exceptions.
  2. To Access different elements of the given array:

    int [] arr1 = {10,69,30,12,11,34,56,78,35}

Write exception-handling statements for ArrayIndexOutOfBounds Exception.

thank you,
regards,
shantuli

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;
};

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

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 ;)

Member Avatar for coil

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;
};

Haha...if OP understands C++ that might actually help him.

In all seriousness though, think about the following:
1. When will division trigger an error (not necessarily just in Java)?
2. What does the ArrayIndexOutOfBoundsException actually refer to?

If you can answer the above, you can solve your problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.