954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Arithmetic Exceptions and exception-handling statements

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

shantuli
Newbie Poster
16 posts since Jul 2004
Reputation Points: 10
Solved Threads: 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;
};
badboy11
Newbie Poster
18 posts since Aug 2009
Reputation Points: 10
Solved Threads: 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

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
 

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.

coil
Posting Whiz in Training
273 posts since Aug 2010
Reputation Points: 27
Solved Threads: 56
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You