?
Solved

When is the ArithmeticException thrown?!

Posted on 2003-02-27
12
Medium Priority
?
501 Views
Last Modified: 2008-02-26
I'm new to Java and I just wanted to know two other situations where an ArithmeticException is thrown other than dividing by zero...
Thanks!
0
Comment
Question by:bakame
12 Comments
 
LVL 2

Expert Comment

by:functionpointer
ID: 8039658
why stop at two?
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8039711
yea where did u get this 'two' figure from
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8039712
yea where did u get this 'two' figure from.
they cud have told what those 'two' are
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 2

Expert Comment

by:functionpointer
ID: 8039771
I got it...

public class Situation
{
   public Situation() {}
   public void doMyHomework(){ throw new ArithmethicException(); }
}

one down, one to go.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8039994
i got the second one
public class IalsoGotSituation
{
  public IalsoGotSituation() {}
  public void IalsoTriedYourHomework(){
 throw new ArithmethicException("this is  the last type");
}
}


btw i have a feeling that this is not homework..
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 8040097
I think this code should throw ArithmeticException :-

double one = 1.0;
double inf = one/0.0;
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8040139
>>> btw i have a feeling that this is not homework..
no? you sure? well, alright then...

try setting the bit length less than 2 constructing a BigInteger ( not much randomness between 1 and 0 ).

also, try taking a BigInteger pow( -1 ), or any negative exponent on a non-float, i suppose.

Both will result in an ArithmeticException.

just think how basic math applies to the data type you are working with. you can find others, i suppose.

0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8040775
If you try to divide any integer number by zero, ArithmeticException occurs.

means

int i=10;
int j;

j = i/0;
0
 

Accepted Solution

by:
vfwood earned 120 total points
ID: 8041894
ArithmeticException can only be thrown by a division by zero, modulo zero, or by code deliberately throwing the exception - i guess thats three ways if you treat modulo and division seperately. This can be done three ways:

a) division operator:
   1 / 0; // throws ArithmeticException - division by zero

b) modulo operator:
   1 % 0; // also throws ArithmeticException (also division by zero though)

c) just felt like throwing it:
   if (a < 1) {
      throw new ArithmeticException("I just felt like it.");
   }
// there - i threw it myself...


maybe this is it?

Vincent Fleetwood
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8046249
ArithmeticException is thrown whenever the data type cant support the requested operation.  

'Division by Zero' just happens to fit this situation with every data type.

Let's not put the cart before the horse here. 3rd grade math tells us not to divide by zero. The question clearly pointed out that division by zero threw the ArithmeticException. He asked for other situations where the exception is thrown. Like I said, a class like BigInteger is full of them.

Limiting math to specific data types limits the operations you can expect to perform on them. When you forget this, you will meet ArithmeticException again.
0
 

Author Comment

by:bakame
ID: 8048273
Thanks... your help is very much appreciated!
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8055281
Interesting placement of the "right" answer, bakame. You are welcome.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month4 days, 11 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question