Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 26th, 2004, 01:49 PM
Registered User
 
Join Date: Feb 2004
Location: Roorkee, Uttranchal, India.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Examples of ArithmeticException

Hello Friends!! I was thinking today for some examples of ArithmeticException other than devide by zero.Can anybody tell me some of such examples?

Sharad Agarwal
  #2 (permalink)  
Old February 27th, 2004, 01:27 PM
Authorized User
 
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by sharadagarwal
 Hello Friends!! I was thinking today for some examples of ArithmeticException other than divide by zero.Can anybody tell me some of such examples?
Dear friend!

Maybe I can think of some (try it out and see if they do cast the ArithmeticException you are looking for):

Math.sqrt(-1), square roots of negative numbers,
- You could create your own package for Complex numbers, and it wouldn't even be difficult, I imagine.. (but probably some university somewhere on the web has done that already, and you just need to find it..)

Divide by zero for (int) and (long) typed numbers,
I mention that again because the floating point (float), (double) types return negative infinity for -1.0/0.0, not a number (NaN) for 0.0/0.0 and positive infinity for 1.0/0.0,
besides: my compiler does allow for direct division of an integer by zero (contrary to what Ivor says in the book). Of course this throws an ArithmeticException, just like the example in the book.
- I will write my own 'alternative algebra' package (when I know Java), that handles zero multiplication and division quite differently (0: I can tell you now that I will derive it from the BigDecimal class, my dear friend :0)

What else? Eh... ArithmeticException hmmm...
1 % 0 will throw one of course, but that is effectively the same as zero division

I think I must leave you with those examples

freeZotic
@ chapter 9 (the dolldrums)



  #3 (permalink)  
Old February 28th, 2004, 02:00 PM
Registered User
 
Join Date: Feb 2004
Location: Roorkee, Uttranchal, India.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks freezotic,
for ur valuable interest that u shown in my question.The example u have given for ArithmeticException seems to work well.I really appreciate it.Thanks friend.

sharad

Sharad Agarwal
  #4 (permalink)  
Old March 5th, 2004, 10:22 AM
Authorized User
 
Join Date: Sep 2003
Location: den Haag, , Netherlands.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by sharadagarwal
 The example u have given for ArithmeticException seems to work well.
But I was wrong in supposing that a complex value like Math.sqrt(-1) would throw an ArithmeticException. It doesn't! Did you notice that?

In fact there is not one method in the java.lang.Math class that would throw an ArithmeticException.
Math.sqrt(-1) and other complex values are given as NaN : not a number.
The funniest is Math.round(NaN) that returns 0

Still friends?

P.S. You could throw an ArithmeticException of your own here and there, that is always possible, e.g.

if(age < 7 && number > 10)
throw new ArithmeticException("Number unsuitable for age group");

I love my Java!
  #5 (permalink)  
Old May 22nd, 2006, 06:49 PM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I looked in the Java Language Specification and the only examples I saw were of divide by zero. Also the modulus operator(%) will throw an Arithmetic Exception if the remainder is zero. One last thing Arithmetic Exception only applies to integers and longs.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
examples raheleh BOOK: Beginning JavaServer Pages 0 October 2nd, 2007 12:17 AM
problems with examples Javahead JSP Basics 8 March 9th, 2004 01:40 PM



All times are GMT -4. The time now is 08:39 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.