943,735 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7865
  • Java RSS
Jul 31st, 2004
0

Arithmetic Exceptions and exception-handling statements

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shantuli is offline Offline
16 posts
since Jul 2004
Aug 26th, 2010
0
Re: Arithmetic Exceptions and exception-handling statements
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:
Java Syntax (Toggle Plain Text)
  1. for ( int i=0; i<max; ++i )
  2. {
  3. if (! (i < 0) || (i >= max) ) { cout << "Data: " arr1[1] << endl;
  4. }
  5. }

The second method is actually using Exception handling:
Java Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. #include <stdexcept>
  4.  
  5. int main()
  6. {
  7. int [] arr1 = {10,69,30,12,11,34,56,78,35};
  8.  
  9. for ( int i=0; i<max; ++i )
  10. {
  11. try{ cout << "Data: " arr1[1] << endl;
  12. }
  13. catch (out_of_range &ex) {
  14. cout << e.what() << endl; //the error message...
  15. }
  16. }
  17.  
  18. return 0;
  19. };
Last edited by badboy11; Aug 26th, 2010 at 5:42 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
badboy11 is offline Offline
17 posts
since Aug 2009
Aug 26th, 2010
0
Re: Arithmetic Exceptions and exception-handling statements
Click to Expand / Collapse  Quote originally posted by badboy11 ...
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:
Java Syntax (Toggle Plain Text)
  1. for ( int i=0; i<max; ++i )
  2. {
  3. if (! (i < 0) || (i >= max) ) { cout << "Data: " arr1[1] << endl;
  4. }
  5. }

The second method is actually using Exception handling:
Java Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. #include <stdexcept>
  4.  
  5. int main()
  6. {
  7. int [] arr1 = {10,69,30,12,11,34,56,78,35};
  8.  
  9. for ( int i=0; i<max; ++i )
  10. {
  11. try{ cout << "Data: " arr1[1] << endl;
  12. }
  13. catch (out_of_range &ex) {
  14. cout << e.what() << endl; //the error message...
  15. }
  16. }
  17.  
  18. return 0;
  19. };
Sounds like Home work.
I don't like to help people earn your degree. They should earn by their efforts.
So show efforts first
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Aug 26th, 2010
0
Re: Arithmetic Exceptions and exception-handling statements
Click to Expand / Collapse  Quote originally posted by badboy11 ...
The second method is actually using Exception handling:
Java Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. #include <stdexcept>
  4.  
  5. int main()
  6. {
  7. int [] arr1 = {10,69,30,12,11,34,56,78,35};
  8.  
  9. for ( int i=0; i<max; ++i )
  10. {
  11. try{ cout << "Data: " arr1[1] << endl;
  12. }
  13. catch (out_of_range &ex) {
  14. cout << e.what() << endl; //the error message...
  15. }
  16. }
  17.  
  18. return 0;
  19. };
This is more funny!
Good work for submitting to a professor teaching you Java
Last edited by evstevemd; Aug 26th, 2010 at 6:00 pm.
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Aug 26th, 2010
0
Re: Arithmetic Exceptions and exception-handling statements
Click to Expand / Collapse  Quote originally posted by badboy11 ...
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:
Java Syntax (Toggle Plain Text)
  1. for ( int i=0; i<max; ++i )
  2. {
  3. if (! (i < 0) || (i >= max) ) { cout << "Data: " arr1[1] << endl;
  4. }
  5. }

The second method is actually using Exception handling:
Java Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. #include <stdexcept>
  4.  
  5. int main()
  6. {
  7. int [] arr1 = {10,69,30,12,11,34,56,78,35};
  8.  
  9. for ( int i=0; i<max; ++i )
  10. {
  11. try{ cout << "Data: " arr1[1] << endl;
  12. }
  13. catch (out_of_range &ex) {
  14. cout << e.what() << endl; //the error message...
  15. }
  16. }
  17.  
  18. return 0;
  19. };
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.
Reputation Points: 27
Solved Threads: 56
Posting Whiz in Training
coil is offline Offline
273 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Inventory Program part1
Next Thread in Java Forum Timeline: javacode





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC