?
Solved

When is the ArithmeticException thrown?!

Posted on 2003-02-27
12
Medium Priority
?
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

800 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