• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error with variable access while handling exception  RSS feed

 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i was just trying run a simple program in exception. Below is my code:





It correctly shows the ArrayIndexOutOfBoundsException every time although by luck i got arithmetic exception. However each time it prints the variable c, it only shows 0. I tried printing the value of a it shows random value as it should. I have not been able to figure out this. Can somebody tell me what exactly is happening?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, have you worked out what the value of the array index is? That shouldn't be too difficult since there appears to be only one place you use an index.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you'd be using better chosen variable names, would find it in a seconds.
How many elements your array holds?
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well, have you worked out what the value of the array index is? That shouldn't be too difficult since there appears to be only one place you use an index.


This is the output i get :


but my question why c shows 0?
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:If you'd be using better chosen variable names, would find it in a seconds.
How many elements your array holds?



No the question is not about the array here. I know array only holds one variable. But the statements :
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using pseudo random generated number.
Lets assume you got generated 13.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more hint: when you divide integers, the remainder gets discarded.
P.S. If you were more patient, probably you would get what you expect with current code, but that could take long.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote: . . . If you were more patient, . . .
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Liutauras Vilda wrote: . . . If you were more patient, . . .


ok ok i got it, it was a silly question, it clicked me why . it will always show 0, if i reverse the value then i get the output i wanted. but seriously thanks.

 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:You're using pseudo random generated number.
Lets assume you got generated 13.


I did assume the fact that a is random but i did not consider the fact that if a>b then obviously output is 0 as in 1/2 is 0. Well i can only do one thing
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhananjay Deshmukh wrote:ok ok i got it, it was a silly question, it clicked me why
What's been told in this forum - "there are no silly questions". It is a learning process I am learning too!

it will always show 0
not always, but only, when generated number is greater than 12. It is difficult to predict how often it would happen, but range could be between 0 and infinity-1 (i think).
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhananjay Deshmukh wrote:
Yesterday, I wrote:
Liutauras Vilda wrote: . . . If you were more patient, . . .


ok ok i got it, . . .
No, you didn't. It means you would expect to run the program 2³² times and even then there is a significant risk of getting the Exception every time.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:but range could be between 0 and infinity-1

What a silly thing I just said here. Dhananjay, ignore that bolded part.
There is the limit for sure, Campbell Ritchie just prompted to me - thank you Campbell.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And in OP's current code exception one or another will be thrown in all ways.
ArrayIndexOutOfBoundsException wouldn't be thrown only when a = 0;
But then ArithmeticException will be - since division by 0 not allowed (since it is int).
As I remember division allowed by 0.0 but then you get "Infinity" - I read about it in "Java in a Nutshell" book if I'm not mistaken.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read all about it in the Java® Language Specification (=JLS). The limits of an int, which is a 32‑bit two's complement integer number, are −2³¹…(+)2³¹ − 1, and the JLS (link needs scrolling down) tells you what those figures are in decimal, octal, hex and binary. The JLS also tells you (LV is correct) that division of integers by 0 causes an Exception and division of non‑zero floating‑point numbers by (±)0.0/(±)0.0f gives you ±∞.
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Liutauras Vilda wrote:but range could be between 0 and infinity-1

What a silly thing I just said here. Dhananjay, ignore that bolded part.
There is the limit for sure, Campbell Ritchie just prompted to me - thank you Campbell.


No I'm serious i get it. The compiler will always consder for ex 12/6663 as 0 which is the output. I didnt think of that and thats why.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!