aspose file tools*
The moose likes Beginning Java and the fly likes why the 0 divident exception not happend? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why the 0 divident exception not happend?" Watch "why the 0 divident exception not happend?" New topic
Author

why the 0 divident exception not happend?

Sufang Hu
Greenhorn

Joined: Dec 22, 2012
Posts: 2
hallo.
I have a java program. I thought c, d and e would trig 0 divident exception, but they did not do so! why?
=====my code begins here==========
class Mainp305 {
public static void main(String[] args) {
int a = 10;
int b = 0;
try{
System.out.println("get into try block");
b++;
int c = a / b;
System.out.println("value of c" + c);
int d = a/b;
System.out.println("value of d" + d);
int e=(a/b);
System.out.println("value of e" + e);
System.out.println("get out of try block");
} catch (Exception e) {
System.out.println("get into catch");
a++;
System.out.println("get out of catch");
} finally {
System.out.println("get into finally");
a++;
System.out.println("get out of finally");
}
System.out.println("a before print:" + a);
System.out.println(a++);
System.out.println("a after print:" + a);
}
}
=======my coce ends here==========

by the way, when i changed my code as following, the 0 divident exception did happen! I don't know why the two programs behaved differently...
=======my changed code: begins here=======
class Mainp305_2 {
public static void main(String[] args) {
int a = 10;
int b = 0;
int c = a / b;
System.out.println("value of c" + c);
}
}
=======my changed code: ends here=======
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Sufang Hu wrote:hallo.
I have a java program. I thought c, d and e would trig 0 divident exception, but they did not do so! why?
=====my code begins here==========

=======my coce ends here==========

by the way, when i changed my code as following, the 0 divident exception did happen! I don't know why the two programs behaved differently...
=======my changed code: begins here=======

=======my changed code: ends here=======



Took a quick look at you code -- your first example does *not* have an divide by zero condition. At which line did you expect a divide by zero to occur?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hint:look at b++;
<edit>Henry beaten me again ;) </edit>

Welcome to JavaRanch Sufang Hu
Sufang Hu
Greenhorn

Joined: Dec 22, 2012
Posts: 2
Thank you for your reply.
I'm sorry. I've just mistaken that b is still 0...
 
wood burning stoves
 
subject: why the 0 divident exception not happend?