0

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

Edited by pyTony: fixed formating

4
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by coil
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;
};

Edited by badboy11: n/a

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

0

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

Edited by Stefano Mtangoo: n/a

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

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.