The Artima Developer Community
Sponsored Link

Java Answers Forum
Exception handling - division by zero (0)

4 replies on 1 page. Most recent reply: Apr 15, 2005 11:05 AM by paul

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
paul

Posts: 7
Nickname: larkinp317
Registered: Mar, 2005

Exception handling - division by zero (0) Posted: Mar 29, 2005 11:24 AM
Reply to this message Reply
Advertisement
Hi I am new to Java and am strugling to complete a script. The script is a basic calculations script that asks the user what type of calculation they want to perform (+-/*). All works so far but I need to make sure that the program pick up when a user is trying to divide by zero. At present I get an 'Infinity' answer. I know I need to use some form of 'try and catch' code but just cannot seam to get it right.

Any help would be appreciated

Thanks

larkinp317


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Exception handling - division by zero (0) Posted: Mar 29, 2005 10:07 PM
Reply to this message Reply
try {
...
} catch (java.lang.ArithmeticException exc) {
}


But this will only be usefu if you want to do the exception handling after the operation.

I would suggest you prevent the division by zero by checking the operators before you execute the operation.

paul

Posts: 7
Nickname: larkinp317
Registered: Mar, 2005

Re: Exception handling - division by zero (0) Posted: Mar 30, 2005 6:06 AM
Reply to this message Reply
Thanks for the tip.

I think you may guess what my next question is, How do you prevent the 'divide by zero'

Thanks

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Exception handling - division by zero (0) Posted: Mar 30, 2005 9:55 PM
Reply to this message Reply
Check if the valuie is 0 before executing the operation.

paul

Posts: 7
Nickname: larkinp317
Registered: Mar, 2005

Re: Exception handling - division by zero (0) Posted: Apr 15, 2005 11:05 AM
Reply to this message Reply
cheers guys, all sorted.

Thanks for the tips

Flat View: This topic has 4 replies on 1 page
Topic: Help need integrating program with MS Access Previous Topic   Next Topic Topic: I want to run this program...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use