Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I am using c# 4.0
I have an issue:


C#
int x=10;
int y =0;
int z=x/y;  //<-- this gives an error at runtime..


however,

C#
int z=10/0;  //<-- this gives an error at compile-time....



May I know why this is the case?

Thanks & Regards,
Posted
Updated 11-Jul-11 1:49am
v2
Comments
Emrah Taylan 11-Jul-11 7:44am    
Because on compile action your ide dont look y declaration is 0 for this reason it gives on runtime

but second example this 0 for this reason it will sow on compile action!

ok?

Hi,

Value in x and y variable setting at run time. Where in 10/0 both are constant, their value setting at compile time.
 
Share this answer
 
Side note to the other answers:
This is dependent how far the parser is "looking" and preprocessing ahead. If you would use Delphi for example, both would have given you a compile time error because it does some serious preprocessing.

Good luck!
 
Share this answer
 
Comments
Manfred Rudolf Bihy 11-Jul-11 8:02am    
As I thouroghly explained in my answer, detecting the first case is not an issue of preprocessing (unless improperly labled thusly by the compiler manufacturer), but rather correctly inferring what the variables values will be at runtime. This can be "guessed" (inferred if you prefer) best for expression sets where definitions and declarations coincide in the same statment.
Parser lookahead also doesn't have the least to do with that.
Sergey Alexandrovich Kryukov 11-Jul-11 21:47pm    
Interesting. I used Delphi from its day zero (and years more of Borland Pascal), but don't remember that. My 5.
I am somewhat doubtful it makes sense -- please see my solution.
--SA
A note to the correct answer by Manfred:

Run-time behavior is not inferred by a good reason: in general case this is impossible. This is the consequence of one of fundamental theorems of computability. If it is not possible based on static analysis in general case, why detecting some cases? Just to create false illusion of safety in developers (and thus provoking errors)?

By that reason, no attempt to statically analyze code to find such problem is done — and this is very good.

As to the expressions like 8/2, the whole expression is treated as one single immediate constant, evaluated during run-time and put in the byte code as one constant, not as expression. Naturally, integer division by zero fails. :-)

A note: pay attention that division by floating-point zero is quite legal and produce valid value of Inf. You can continue using the result; for example, 1/Inf will be evaluated as 0.

—SA
 
Share this answer
 
Comments
Espen Harlinn 12-Jul-11 9:56am    
Good points - my 5
Sergey Alexandrovich Kryukov 12-Jul-11 10:36am    
Thank you, Espen.
--SA
That is because the first error can be only be reliably detected at runtime. The second error is detected at compile time because the compiler could already execute the division when compiling the program. Like when this is done for optimization purposes. The first case poses a problem for the compiler because it is hard/impossible to infer the correct runtime condition just from the given code. Actually in your case where the declaration coincides with the definition the compile should be able to detect this case also. I think the real reason why this is not checked is that this is a very trivial case where runtime behaviour could be infered, whereas it would be impossible in most all of the other possibilities.

Cheers!

—MRB
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jul-11 21:45pm    
Correct, my 5. I've added my additional notes to your answer -- please see my solution.
--SA
Espen Harlinn 12-Jul-11 9:56am    
Nice reply Manfred, my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900