LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2004, 09:24 AM   #1
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Rep: Reputation: 15
ArithmeticException missing with Java?


Just to have your point of view...

I am performing a division so instead of testing the nullity of my denominator, I thought it was smarter (and faster) to catch an ArithmeticException and set the result of the division to zero if it would occur. Unfortunately, there's nothing thrown....

Do you know why?

PS: it's not important while I added a test but I would like to understand.
 
Old 04-13-2004, 09:43 AM   #2
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
If the denominator is 0, it will throw ArithmeticException.
Check your code again.
Or run the test program

public class Test {
public static void main(String[] args) {

int a = 1;
int b = 0;
int result = 0;

try {
result= a/b;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException, set result to 0");
result = 0;
}

System.out.println(result);

}
}
 
Old 04-13-2004, 09:59 AM   #3
pycoucou
Member
 
Registered: Apr 2004
Location: Edinburgh
Posts: 78

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by moeminhtun
Or run the test program
Alright... the test program gives me the exception. The error seems to occur for a and b double. Why? The result in this case is infinity. The program I used to run gave me NaN. Do you have an idea why there are such different behaviours?
 
Old 04-13-2004, 11:36 AM   #4
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
Then your program must be performing the floating point calculation.
In Java, only the Integer division by zero, including modulo(%) operation, results in an ArithmeticException. If floating point, it won't throw any exception and it will return a NaN.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox + Java (*Missing Plugins? wtf*) itz2000 Linux - Software 20 11-02-2005 11:34 AM
java on firefox path missing? barrys Mandriva 10 03-23-2004 08:02 AM
missing java swing libraries? a1ghagh05t Programming 5 01-26-2004 06:04 AM
Polish characters missing in Java apps zgolus Linux - Software 0 12-03-2003 04:23 AM
national characters missing in java applications zgolus Slackware 0 12-01-2003 03:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration