Why does int i = 10; i / 0; compile, but 5 / 0 gives CS0020 - Division by constant zero? [duplicate] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Why does the compiler not show an error when we try to divide a variable by zeroCase insensitive 'Contains(string)'Why can't I unbox an int as a decimal?Curious null-coalescing operator custom implicit conversion behaviourIs there a reason for C#'s reuse of the variable in a foreach?Why does the C# compiler allow an explicit cast between IEnumerable<T> and TAlmostAnything?Int vs Double and divide by zero exceptionWhy does the compiler not show an error when we try to divide a variable by zeroWhy not inherit from List<T>?Weird division by zero errorWhy do decimals give a compile time error on division by zero?
Is this Kuo-toa homebrew race balanced?
How do you write "wild blueberries flavored"?
Does the transliteration of 'Dravidian' exist in Hindu scripture? Does 'Dravida' refer to a Geographical area or an ethnic group?
Is there any significance to the prison numbers of the Beagle Boys starting with 176-?
Relating to the President and obstruction, were Mueller's conclusions preordained?
Why BitLocker does not use RSA
How to ask rejected full-time candidates to apply to teach individual courses?
Why shouldn't this prove the Prime Number Theorem?
A proverb that is used to imply that you have unexpectedly faced a big problem
Was the pager message from Nick Fury to Captain Marvel unnecessary?
Sally's older brother
Keyboard layout stuck into CZ_german no english layout after update, restore into original EN_us and EL_Gr ones
How to evaluate this function?
Can two people see the same photon?
latest version of QGIS fails to edit attribute table of GeoJSON file
As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?
The test team as an enemy of development? And how can this be avoided?
My mentor says to set image to Fine instead of RAW — how is this different from JPG?
NIntegrate on a solution of a matrix ODE
Understanding piped command in Gnu/Linux
How to make triangles with rounded sides and corners? (squircle with 3 sides)
Pointing to problems without suggesting solutions
New Order #6: Easter Egg
How much damage would a cupful of neutron star matter do to the Earth?
Why does int i = 10; i / 0; compile, but 5 / 0 gives CS0020 - Division by constant zero? [duplicate]
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Why does the compiler not show an error when we try to divide a variable by zeroCase insensitive 'Contains(string)'Why can't I unbox an int as a decimal?Curious null-coalescing operator custom implicit conversion behaviourIs there a reason for C#'s reuse of the variable in a foreach?Why does the C# compiler allow an explicit cast between IEnumerable<T> and TAlmostAnything?Int vs Double and divide by zero exceptionWhy does the compiler not show an error when we try to divide a variable by zeroWhy not inherit from List<T>?Weird division by zero errorWhy do decimals give a compile time error on division by zero?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Why does the compiler not show an error when we try to divide a variable by zero
5 answers
Consider the following snippet:
int i = 5 / 0;
This gives compiler error CS0020: Division by constant zero, which is fine. However, the next snippet:
int i = 10;
i = i / 0;
Compiles just fine.
Does someone know why? I see no reason why the compiler allows an integer variable to be divided by a zero integer constant.
c#
marked as duplicate by Servy
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 13:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 3 more comments
This question already has an answer here:
Why does the compiler not show an error when we try to divide a variable by zero
5 answers
Consider the following snippet:
int i = 5 / 0;
This gives compiler error CS0020: Division by constant zero, which is fine. However, the next snippet:
int i = 10;
i = i / 0;
Compiles just fine.
Does someone know why? I see no reason why the compiler allows an integer variable to be divided by a zero integer constant.
c#
marked as duplicate by Servy
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 13:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
I believe it is the difference between the compile time evaluation of the expression and the runtime one.
– meJustAndrew
Mar 22 at 12:42
4
Isn't 5 / 0 a constant during compilation while i / 0 is not?
– Christian Ivicevic
Mar 22 at 12:42
3
Two identical questions in a couple of minutes?
– Panagiotis Kanavos
Mar 22 at 12:43
1
@PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D
– Charles May
Mar 22 at 12:47
@PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.
– Stilgar
Mar 22 at 12:48
|
show 3 more comments
This question already has an answer here:
Why does the compiler not show an error when we try to divide a variable by zero
5 answers
Consider the following snippet:
int i = 5 / 0;
This gives compiler error CS0020: Division by constant zero, which is fine. However, the next snippet:
int i = 10;
i = i / 0;
Compiles just fine.
Does someone know why? I see no reason why the compiler allows an integer variable to be divided by a zero integer constant.
c#
This question already has an answer here:
Why does the compiler not show an error when we try to divide a variable by zero
5 answers
Consider the following snippet:
int i = 5 / 0;
This gives compiler error CS0020: Division by constant zero, which is fine. However, the next snippet:
int i = 10;
i = i / 0;
Compiles just fine.
Does someone know why? I see no reason why the compiler allows an integer variable to be divided by a zero integer constant.
This question already has an answer here:
Why does the compiler not show an error when we try to divide a variable by zero
5 answers
c#
c#
edited Mar 22 at 12:56
meJustAndrew
3,22742552
3,22742552
asked Mar 22 at 12:38
NickNick
2,3671215
2,3671215
marked as duplicate by Servy
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 13:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Servy
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 13:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
I believe it is the difference between the compile time evaluation of the expression and the runtime one.
– meJustAndrew
Mar 22 at 12:42
4
Isn't 5 / 0 a constant during compilation while i / 0 is not?
– Christian Ivicevic
Mar 22 at 12:42
3
Two identical questions in a couple of minutes?
– Panagiotis Kanavos
Mar 22 at 12:43
1
@PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D
– Charles May
Mar 22 at 12:47
@PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.
– Stilgar
Mar 22 at 12:48
|
show 3 more comments
4
I believe it is the difference between the compile time evaluation of the expression and the runtime one.
– meJustAndrew
Mar 22 at 12:42
4
Isn't 5 / 0 a constant during compilation while i / 0 is not?
– Christian Ivicevic
Mar 22 at 12:42
3
Two identical questions in a couple of minutes?
– Panagiotis Kanavos
Mar 22 at 12:43
1
@PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D
– Charles May
Mar 22 at 12:47
@PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.
– Stilgar
Mar 22 at 12:48
4
4
I believe it is the difference between the compile time evaluation of the expression and the runtime one.
– meJustAndrew
Mar 22 at 12:42
I believe it is the difference between the compile time evaluation of the expression and the runtime one.
– meJustAndrew
Mar 22 at 12:42
4
4
Isn't 5 / 0 a constant during compilation while i / 0 is not?
– Christian Ivicevic
Mar 22 at 12:42
Isn't 5 / 0 a constant during compilation while i / 0 is not?
– Christian Ivicevic
Mar 22 at 12:42
3
3
Two identical questions in a couple of minutes?
– Panagiotis Kanavos
Mar 22 at 12:43
Two identical questions in a couple of minutes?
– Panagiotis Kanavos
Mar 22 at 12:43
1
1
@PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D
– Charles May
Mar 22 at 12:47
@PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D
– Charles May
Mar 22 at 12:47
@PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.
– Stilgar
Mar 22 at 12:48
@PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.
– Stilgar
Mar 22 at 12:48
|
show 3 more comments
4 Answers
4
active
oldest
votes
In the general case, the compiler has no reason to disallow division by zero (or any other number) at runtime.
Your first example, though, is a compile time constant, i.e. it's calculated by the compiler and replaced with the result of the evaluation. This is what the compiler's complaining about as it rightly doesn't know what integer value to put in place of 5/0.
1
They could have made it so this case generates code to throw DBZ exception at runtime.
– Stilgar
Mar 22 at 12:47
6
This doesn't hold foranyInt / 0: the compiler could know it's always illegal.
– Henk Holterman
Mar 22 at 12:47
@HenkHolterman in what case is it not true for anyInt / 0?
– Stilgar
Mar 22 at 12:53
3
@HenkHolterman: given C#'s highly flexible rules for implicit conversions and operator overloading, I suspect the very specific case of someone writing down a literalx / 0expression withxan integral type didn't warrant a special check to issue a warning/error for that (and only that). It just leaves non-constant expressions to the runtime, always.
– Jeroen Mostert
Mar 22 at 12:55
2
@Stilgar I think it's one of those "where would you draw the line?" cases. Checking for division of an integral type by a constant integral zero is simple enough, but then do you check for zero in a locally assigned variable? A parameter? Given how unlikely it is that a developer is going to putx / 0directly in their code (and that they'll find it when they run the program), it's not worth the effort. In the compile-time case, the check is mandatory because it's not compilable as-is.
– Philip C
Mar 22 at 13:14
|
show 3 more comments
It is simply the difference between the compile time check and the runtime check. While
i = i / 0;
throws
System.DivideByZeroException: 'Attempted to divide by zero.'
the
i = 5 / 0;
gives a compilation error
Error CS0020 Division by constant zero.
The value of 5 / 0 is evaluated at compile time, this is what makes the difference between receiving an error and getting an exception.
EDIT:
Given the comments to the answers here, I will take some time to write an assumption of why the compiler works this way. The compiler was never intended to prevent division by zero of a variable. If you want to do it, you are allowed to, and you could work in some sneaky ways to get your program dividing by zero. What was the compiler supposed to do is just making some optimization when it can, by computing it advance some calculus and replace them with the resulting value. When it sees the division by zero, it just gives you an error as it can not compute it.
1
We know what it does, we are wondering why it was made this way.
– Stilgar
Mar 22 at 12:46
add a comment |
Because of the compiler converts int i = 5 / 0; to a constant value , so it must calculate the value during compile time, which gives it an error.
But the code
int i = 10;
i = i / 0;
is formally correct. It throws an exception and it is your decision and your intention to generate such an exception.
They could have made it so both throw exception or both give an error but they decided not to. The question is why they chose this specific and slightly inconsistent behavior.
– Stilgar
Mar 22 at 12:58
add a comment |
same as everyone saying here.
This line here is compile time and since compiler doesn't know the answer of this.It is giving error
int i = 5 / 0;
where as this line is a run time check and its just a general mathematical calculation so compiler no reason to throw this as an error
int i = 10;
i = i / 0;
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the general case, the compiler has no reason to disallow division by zero (or any other number) at runtime.
Your first example, though, is a compile time constant, i.e. it's calculated by the compiler and replaced with the result of the evaluation. This is what the compiler's complaining about as it rightly doesn't know what integer value to put in place of 5/0.
1
They could have made it so this case generates code to throw DBZ exception at runtime.
– Stilgar
Mar 22 at 12:47
6
This doesn't hold foranyInt / 0: the compiler could know it's always illegal.
– Henk Holterman
Mar 22 at 12:47
@HenkHolterman in what case is it not true for anyInt / 0?
– Stilgar
Mar 22 at 12:53
3
@HenkHolterman: given C#'s highly flexible rules for implicit conversions and operator overloading, I suspect the very specific case of someone writing down a literalx / 0expression withxan integral type didn't warrant a special check to issue a warning/error for that (and only that). It just leaves non-constant expressions to the runtime, always.
– Jeroen Mostert
Mar 22 at 12:55
2
@Stilgar I think it's one of those "where would you draw the line?" cases. Checking for division of an integral type by a constant integral zero is simple enough, but then do you check for zero in a locally assigned variable? A parameter? Given how unlikely it is that a developer is going to putx / 0directly in their code (and that they'll find it when they run the program), it's not worth the effort. In the compile-time case, the check is mandatory because it's not compilable as-is.
– Philip C
Mar 22 at 13:14
|
show 3 more comments
In the general case, the compiler has no reason to disallow division by zero (or any other number) at runtime.
Your first example, though, is a compile time constant, i.e. it's calculated by the compiler and replaced with the result of the evaluation. This is what the compiler's complaining about as it rightly doesn't know what integer value to put in place of 5/0.
1
They could have made it so this case generates code to throw DBZ exception at runtime.
– Stilgar
Mar 22 at 12:47
6
This doesn't hold foranyInt / 0: the compiler could know it's always illegal.
– Henk Holterman
Mar 22 at 12:47
@HenkHolterman in what case is it not true for anyInt / 0?
– Stilgar
Mar 22 at 12:53
3
@HenkHolterman: given C#'s highly flexible rules for implicit conversions and operator overloading, I suspect the very specific case of someone writing down a literalx / 0expression withxan integral type didn't warrant a special check to issue a warning/error for that (and only that). It just leaves non-constant expressions to the runtime, always.
– Jeroen Mostert
Mar 22 at 12:55
2
@Stilgar I think it's one of those "where would you draw the line?" cases. Checking for division of an integral type by a constant integral zero is simple enough, but then do you check for zero in a locally assigned variable? A parameter? Given how unlikely it is that a developer is going to putx / 0directly in their code (and that they'll find it when they run the program), it's not worth the effort. In the compile-time case, the check is mandatory because it's not compilable as-is.
– Philip C
Mar 22 at 13:14
|
show 3 more comments
In the general case, the compiler has no reason to disallow division by zero (or any other number) at runtime.
Your first example, though, is a compile time constant, i.e. it's calculated by the compiler and replaced with the result of the evaluation. This is what the compiler's complaining about as it rightly doesn't know what integer value to put in place of 5/0.
In the general case, the compiler has no reason to disallow division by zero (or any other number) at runtime.
Your first example, though, is a compile time constant, i.e. it's calculated by the compiler and replaced with the result of the evaluation. This is what the compiler's complaining about as it rightly doesn't know what integer value to put in place of 5/0.
answered Mar 22 at 12:43
Philip CPhilip C
1,1822144
1,1822144
1
They could have made it so this case generates code to throw DBZ exception at runtime.
– Stilgar
Mar 22 at 12:47
6
This doesn't hold foranyInt / 0: the compiler could know it's always illegal.
– Henk Holterman
Mar 22 at 12:47
@HenkHolterman in what case is it not true for anyInt / 0?
– Stilgar
Mar 22 at 12:53
3
@HenkHolterman: given C#'s highly flexible rules for implicit conversions and operator overloading, I suspect the very specific case of someone writing down a literalx / 0expression withxan integral type didn't warrant a special check to issue a warning/error for that (and only that). It just leaves non-constant expressions to the runtime, always.
– Jeroen Mostert
Mar 22 at 12:55
2
@Stilgar I think it's one of those "where would you draw the line?" cases. Checking for division of an integral type by a constant integral zero is simple enough, but then do you check for zero in a locally assigned variable? A parameter? Given how unlikely it is that a developer is going to putx / 0directly in their code (and that they'll find it when they run the program), it's not worth the effort. In the compile-time case, the check is mandatory because it's not compilable as-is.
– Philip C
Mar 22 at 13:14
|
show 3 more comments
1
They could have made it so this case generates code to throw DBZ exception at runtime.
– Stilgar
Mar 22 at 12:47
6
This doesn't hold foranyInt / 0: the compiler could know it's always illegal.
– Henk Holterman
Mar 22 at 12:47
@HenkHolterman in what case is it not true for anyInt / 0?
– Stilgar
Mar 22 at 12:53
3
@HenkHolterman: given C#'s highly flexible rules for implicit conversions and operator overloading, I suspect the very specific case of someone writing down a literalx / 0expression withxan integral type didn't warrant a special check to issue a warning/error for that (and only that). It just leaves non-constant expressions to the runtime, always.
– Jeroen Mostert
Mar 22 at 12:55
2
@Stilgar I think it's one of those "where would you draw the line?" cases. Checking for division of an integral type by a constant integral zero is simple enough, but then do you check for zero in a locally assigned variable? A parameter? Given how unlikely it is that a developer is going to putx / 0directly in their code (and that they'll find it when they run the program), it's not worth the effort. In the compile-time case, the check is mandatory because it's not compilable as-is.
– Philip C
Mar 22 at 13:14
1
1
They could have made it so this case generates code to throw DBZ exception at runtime.
– Stilgar
Mar 22 at 12:47
They could have made it so this case generates code to throw DBZ exception at runtime.
– Stilgar
Mar 22 at 12:47
6
6
This doesn't hold for
anyInt / 0 : the compiler could know it's always illegal.– Henk Holterman
Mar 22 at 12:47
This doesn't hold for
anyInt / 0 : the compiler could know it's always illegal.– Henk Holterman
Mar 22 at 12:47
@HenkHolterman in what case is it not true for anyInt / 0?
– Stilgar
Mar 22 at 12:53
@HenkHolterman in what case is it not true for anyInt / 0?
– Stilgar
Mar 22 at 12:53
3
3
@HenkHolterman: given C#'s highly flexible rules for implicit conversions and operator overloading, I suspect the very specific case of someone writing down a literal
x / 0 expression with x an integral type didn't warrant a special check to issue a warning/error for that (and only that). It just leaves non-constant expressions to the runtime, always.– Jeroen Mostert
Mar 22 at 12:55
@HenkHolterman: given C#'s highly flexible rules for implicit conversions and operator overloading, I suspect the very specific case of someone writing down a literal
x / 0 expression with x an integral type didn't warrant a special check to issue a warning/error for that (and only that). It just leaves non-constant expressions to the runtime, always.– Jeroen Mostert
Mar 22 at 12:55
2
2
@Stilgar I think it's one of those "where would you draw the line?" cases. Checking for division of an integral type by a constant integral zero is simple enough, but then do you check for zero in a locally assigned variable? A parameter? Given how unlikely it is that a developer is going to put
x / 0 directly in their code (and that they'll find it when they run the program), it's not worth the effort. In the compile-time case, the check is mandatory because it's not compilable as-is.– Philip C
Mar 22 at 13:14
@Stilgar I think it's one of those "where would you draw the line?" cases. Checking for division of an integral type by a constant integral zero is simple enough, but then do you check for zero in a locally assigned variable? A parameter? Given how unlikely it is that a developer is going to put
x / 0 directly in their code (and that they'll find it when they run the program), it's not worth the effort. In the compile-time case, the check is mandatory because it's not compilable as-is.– Philip C
Mar 22 at 13:14
|
show 3 more comments
It is simply the difference between the compile time check and the runtime check. While
i = i / 0;
throws
System.DivideByZeroException: 'Attempted to divide by zero.'
the
i = 5 / 0;
gives a compilation error
Error CS0020 Division by constant zero.
The value of 5 / 0 is evaluated at compile time, this is what makes the difference between receiving an error and getting an exception.
EDIT:
Given the comments to the answers here, I will take some time to write an assumption of why the compiler works this way. The compiler was never intended to prevent division by zero of a variable. If you want to do it, you are allowed to, and you could work in some sneaky ways to get your program dividing by zero. What was the compiler supposed to do is just making some optimization when it can, by computing it advance some calculus and replace them with the resulting value. When it sees the division by zero, it just gives you an error as it can not compute it.
1
We know what it does, we are wondering why it was made this way.
– Stilgar
Mar 22 at 12:46
add a comment |
It is simply the difference between the compile time check and the runtime check. While
i = i / 0;
throws
System.DivideByZeroException: 'Attempted to divide by zero.'
the
i = 5 / 0;
gives a compilation error
Error CS0020 Division by constant zero.
The value of 5 / 0 is evaluated at compile time, this is what makes the difference between receiving an error and getting an exception.
EDIT:
Given the comments to the answers here, I will take some time to write an assumption of why the compiler works this way. The compiler was never intended to prevent division by zero of a variable. If you want to do it, you are allowed to, and you could work in some sneaky ways to get your program dividing by zero. What was the compiler supposed to do is just making some optimization when it can, by computing it advance some calculus and replace them with the resulting value. When it sees the division by zero, it just gives you an error as it can not compute it.
1
We know what it does, we are wondering why it was made this way.
– Stilgar
Mar 22 at 12:46
add a comment |
It is simply the difference between the compile time check and the runtime check. While
i = i / 0;
throws
System.DivideByZeroException: 'Attempted to divide by zero.'
the
i = 5 / 0;
gives a compilation error
Error CS0020 Division by constant zero.
The value of 5 / 0 is evaluated at compile time, this is what makes the difference between receiving an error and getting an exception.
EDIT:
Given the comments to the answers here, I will take some time to write an assumption of why the compiler works this way. The compiler was never intended to prevent division by zero of a variable. If you want to do it, you are allowed to, and you could work in some sneaky ways to get your program dividing by zero. What was the compiler supposed to do is just making some optimization when it can, by computing it advance some calculus and replace them with the resulting value. When it sees the division by zero, it just gives you an error as it can not compute it.
It is simply the difference between the compile time check and the runtime check. While
i = i / 0;
throws
System.DivideByZeroException: 'Attempted to divide by zero.'
the
i = 5 / 0;
gives a compilation error
Error CS0020 Division by constant zero.
The value of 5 / 0 is evaluated at compile time, this is what makes the difference between receiving an error and getting an exception.
EDIT:
Given the comments to the answers here, I will take some time to write an assumption of why the compiler works this way. The compiler was never intended to prevent division by zero of a variable. If you want to do it, you are allowed to, and you could work in some sneaky ways to get your program dividing by zero. What was the compiler supposed to do is just making some optimization when it can, by computing it advance some calculus and replace them with the resulting value. When it sees the division by zero, it just gives you an error as it can not compute it.
edited Mar 22 at 12:53
answered Mar 22 at 12:45
meJustAndrewmeJustAndrew
3,22742552
3,22742552
1
We know what it does, we are wondering why it was made this way.
– Stilgar
Mar 22 at 12:46
add a comment |
1
We know what it does, we are wondering why it was made this way.
– Stilgar
Mar 22 at 12:46
1
1
We know what it does, we are wondering why it was made this way.
– Stilgar
Mar 22 at 12:46
We know what it does, we are wondering why it was made this way.
– Stilgar
Mar 22 at 12:46
add a comment |
Because of the compiler converts int i = 5 / 0; to a constant value , so it must calculate the value during compile time, which gives it an error.
But the code
int i = 10;
i = i / 0;
is formally correct. It throws an exception and it is your decision and your intention to generate such an exception.
They could have made it so both throw exception or both give an error but they decided not to. The question is why they chose this specific and slightly inconsistent behavior.
– Stilgar
Mar 22 at 12:58
add a comment |
Because of the compiler converts int i = 5 / 0; to a constant value , so it must calculate the value during compile time, which gives it an error.
But the code
int i = 10;
i = i / 0;
is formally correct. It throws an exception and it is your decision and your intention to generate such an exception.
They could have made it so both throw exception or both give an error but they decided not to. The question is why they chose this specific and slightly inconsistent behavior.
– Stilgar
Mar 22 at 12:58
add a comment |
Because of the compiler converts int i = 5 / 0; to a constant value , so it must calculate the value during compile time, which gives it an error.
But the code
int i = 10;
i = i / 0;
is formally correct. It throws an exception and it is your decision and your intention to generate such an exception.
Because of the compiler converts int i = 5 / 0; to a constant value , so it must calculate the value during compile time, which gives it an error.
But the code
int i = 10;
i = i / 0;
is formally correct. It throws an exception and it is your decision and your intention to generate such an exception.
edited Mar 22 at 13:01
IDarkCoder
362111
362111
answered Mar 22 at 12:49
Piotr StappPiotr Stapp
14.8k44992
14.8k44992
They could have made it so both throw exception or both give an error but they decided not to. The question is why they chose this specific and slightly inconsistent behavior.
– Stilgar
Mar 22 at 12:58
add a comment |
They could have made it so both throw exception or both give an error but they decided not to. The question is why they chose this specific and slightly inconsistent behavior.
– Stilgar
Mar 22 at 12:58
They could have made it so both throw exception or both give an error but they decided not to. The question is why they chose this specific and slightly inconsistent behavior.
– Stilgar
Mar 22 at 12:58
They could have made it so both throw exception or both give an error but they decided not to. The question is why they chose this specific and slightly inconsistent behavior.
– Stilgar
Mar 22 at 12:58
add a comment |
same as everyone saying here.
This line here is compile time and since compiler doesn't know the answer of this.It is giving error
int i = 5 / 0;
where as this line is a run time check and its just a general mathematical calculation so compiler no reason to throw this as an error
int i = 10;
i = i / 0;
add a comment |
same as everyone saying here.
This line here is compile time and since compiler doesn't know the answer of this.It is giving error
int i = 5 / 0;
where as this line is a run time check and its just a general mathematical calculation so compiler no reason to throw this as an error
int i = 10;
i = i / 0;
add a comment |
same as everyone saying here.
This line here is compile time and since compiler doesn't know the answer of this.It is giving error
int i = 5 / 0;
where as this line is a run time check and its just a general mathematical calculation so compiler no reason to throw this as an error
int i = 10;
i = i / 0;
same as everyone saying here.
This line here is compile time and since compiler doesn't know the answer of this.It is giving error
int i = 5 / 0;
where as this line is a run time check and its just a general mathematical calculation so compiler no reason to throw this as an error
int i = 10;
i = i / 0;
answered Mar 22 at 13:32
dark_3nergydark_3nergy
628
628
add a comment |
add a comment |
4
I believe it is the difference between the compile time evaluation of the expression and the runtime one.
– meJustAndrew
Mar 22 at 12:42
4
Isn't 5 / 0 a constant during compilation while i / 0 is not?
– Christian Ivicevic
Mar 22 at 12:42
3
Two identical questions in a couple of minutes?
– Panagiotis Kanavos
Mar 22 at 12:43
1
@PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D
– Charles May
Mar 22 at 12:47
@PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.
– Stilgar
Mar 22 at 12:48