This discussion is archived
4 Replies Latest reply: Dec 27, 2012 1:30 AM by gimbal2 RSS

why no exception happened?

981358 Newbie
Currently Being Moderated
hallo, everyone here.
I've a question about 0 divident exception.
I wrote the following code, where c, d, and e should have triggered 0 divident exceptions, but in fact no exception happend. Why?

==========my code begins=================
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 code ends============

by the way, after I change my code as following, when I executed it, the 0 divident exception did happened. I don't knowd why.
Please do help me. Thank you.
======my changed code, begins==========
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============
  • 1. Re: why no exception happened?
    Kayaman Guru
    Currently Being Moderated
    You increment b to 1 in the beginning of your code. You're dividing by 1, not 0.
  • 2. Re: why no exception happened?
    rp0428 Guru
    Currently Being Moderated
    >
    I've a question about 0 divident exception.
    I wrote the following code, where c, d, and e should have triggered 0 divident exceptions, but in fact no exception happend. Why?
    >
    Because a 'divide by zero' exception only occurs when you attempt to 'divide by zero'.

    This line of code sets 'b' to zero
    nt b = 0;
    And this line of code increments 'b' to 1
    b++;
    So these lines of code are dividing by 1
    int c = a / b;
    int d = a/b;
    int e=(a/b);
    Dividing by 1 does not cause a 'divide by zero' exception.
    >
    after I change my code as following, when I executed it, the 0 divident exception did happened. I don't knowd why.
    >
    Because this line of code sets 'b' to 0
    int b = 0;
    Anid this line of code tries to divide by 0
    int c = a / b;
    Because a 'divide by zero' exception occurs when you attempt to 'divide by zero'.

    For the Java spec for the postfix operator (++) see the Java Language Specification
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2
    >
    15.14.2. Postfix Increment Operator ++
    . . .
    the value 1 is added to the value of the variable and the sum is stored back into the variable.
    . . .
  • 3. Re: why no exception happened?
    aksarben Journeyer
    Currently Being Moderated
    General Tip: Invest a few hours in learning to use a debugger. All IDE's come with them. Stepping through the code in a debugger & watching variable values change will make the answers to questions like this immediately obvious.

    Without a debugger, you have to track variable values mentally, and if you're like me, you'll invariably overlook something, especially as the code grows more complex.
  • 4. Re: why no exception happened?
    gimbal2 Guru
    Currently Being Moderated
    Debuggers are great if you want to like them (which some people do not for some reason), but the OP was already using the "spam system out" trick to not have to remember variable state. You don't -have- to use a debugger, sometimes its just easier to let the code print out what its doing and then examine that. Less mouse clicking that way :)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points