Quick ArithmeticException question

Started by
4 comments, last by frob 17 years, 8 months ago
Hi there people, I was just wondering if there were any other instances, other than devide by zero, that an ArithmeticException will be thrown. The reason I ask is that one is being thrown while running my game on a series 40 phone (7210, 7250, 3300) the device is throwing the exception while displaying a fairly innocent screen, and I've made allowances in any calculations to make sure I don't devide by zero. As with all good bugs in J2ME, the exception isn't thrown in the emulator (7210 sdk emulator) and isn't thrown on series 60, or Sony Ericsson devices either. To add to the fun, the exception is only thrown 9 out of ten times. Sometimes it works just fine. Could it have anything to do with having a small amount of free heap available? Thanks for reading!
Advertisement
have you pinpointed the code that throws the exception? can you post it?

if you believe that it's related to low memory, you may play with the -heapsize parameter of the emulator - 210000 would be a value to try. also, dont forget to enable -traceexceptions.
I havn't been able to pinpoint what area of code is causing it very accurately because it only occurs on the device, so i can only narrow it down to 'some code in one of three classes'.

Although, I've just done a new build and it seems to have norrowed it down slightly, something to do with a scrollbar on a screen that has more than a screen's worth of text (i.e. a screen with scrollable text)

I'm going to try and disable that and see what we can see.

As for thinking its low memory, I've tried running it on an emulator with -heapsize 190000 and it still works fine. So i#m pretty sure it's not a memory issue.

And finally, enabling -traceexceptions won't help much I'm afraid since the emulator doesn't throw the exception mentioned.

So is 'devide by zero' the only thing that throws an ArithmeticException or are there other instances where it would be thrown?
afaik, that exception occurs only when divide by zero using / operator or % (modulo) operator.
When grepping through the HTML pages of the Java API, the only thing I've found was java.math.RoundingMode.UNNECESSARY that may cause ArithmeticException, too. All other possible sources (negative arguments to even roots or logarithms, arc sine/cosine/tangens... w/ arguments outside [-1,+1] and so on) seem to return NaNs (since complex numbers cannot be returned).

Also possible is matrix inversion of a singular matrix, but I think that the OP has these things under its own control.
Several third party and specialized javax.* libraries will throw it as well.

For example, some javax.realtime methods can throw it when converting time values.

This topic is closed to new replies.

Advertisement