This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why the 0 divident exception not happend?

 
Sufang Hu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint:look at b++;
<edit>Henry beaten me again ;) </edit>

Welcome to JavaRanch Sufang Hu
 
Sufang Hu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.
I'm sorry. I've just mistaken that b is still 0...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic