Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 26th, 2004, 01:49 PM
Registered User
 
Join Date: Feb 2004
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
 
Old February 27th, 2004, 01:27 PM
Authorized User
 
Join Date: Sep 2003
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)



 
Old February 28th, 2004, 02:00 PM
Registered User
 
Join Date: Feb 2004
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
 
Old March 5th, 2004, 10:22 AM
Authorized User
 
Join Date: Sep 2003
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!
 
Old May 22nd, 2006, 06:49 PM
Registered User
 
Join Date: May 2006
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.






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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.