Why does int i = 10; i / 0; compile, but 5 / 0 gives CS0020 - Division by constant zero? [duplicate]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?

Travelling outside the UK without a passport

How do I find all files that end with a dot

C++ debug/print custom type with GDB : the case of nlohmann json library

Biological Blimps: Propulsion

GraphicsGrid with a Label for each Column and Row

Lowest total scrabble score

Open a doc from terminal, but not by its name

Offered money to buy a house, seller is asking for more to cover gap between their listing and mortgage owed

Delivering sarcasm

Does a 'pending' US visa application constitute a denial?

Why does the Sun have different day lengths, but not the gas giants?

Is it improper etiquette to ask your opponent what his/her rating is before the game?

How much character growth crosses the line into breaking the character

What are the purposes of autoencoders?

Why is it that I can sometimes guess the next note?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Why electric field inside a cavity of a non-conducting sphere not zero?

Pre-mixing cryogenic fuels and using only one fuel tank

Non-trope happy ending?

Count the occurrence of each unique word in the file

Create all possible words using a set or letters

If a character has darkvision, can they see through an area of nonmagical darkness filled with lightly obscuring gas?

Are the IPv6 address space and IPv4 address space completely disjoint?

Fear of getting stuck on one programming language / technology that is not used in my country



Why does int i = 10; i / 0; compile, but 5 / 0 gives CS0020 - Division by constant zero? [duplicate]


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?













14
















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.










share|improve this question















marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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();

);
);
);
yesterday


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
    yesterday






  • 3





    Isn't 5 / 0 a constant during compilation while i / 0 is not?

    – Christian Ivicevic
    yesterday






  • 3





    Two identical questions in a couple of minutes?

    – Panagiotis Kanavos
    yesterday






  • 1





    @PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D

    – Charles May
    yesterday











  • @PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.

    – Stilgar
    yesterday















14
















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.










share|improve this question















marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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();

);
);
);
yesterday


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
    yesterday






  • 3





    Isn't 5 / 0 a constant during compilation while i / 0 is not?

    – Christian Ivicevic
    yesterday






  • 3





    Two identical questions in a couple of minutes?

    – Panagiotis Kanavos
    yesterday






  • 1





    @PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D

    – Charles May
    yesterday











  • @PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.

    – Stilgar
    yesterday













14












14








14


1







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.










share|improve this question

















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#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









meJustAndrew

3,15932452




3,15932452










asked yesterday









NickNick

2,1721215




2,1721215




marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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();

);
);
);
yesterday


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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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();

);
);
);
yesterday


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
    yesterday






  • 3





    Isn't 5 / 0 a constant during compilation while i / 0 is not?

    – Christian Ivicevic
    yesterday






  • 3





    Two identical questions in a couple of minutes?

    – Panagiotis Kanavos
    yesterday






  • 1





    @PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D

    – Charles May
    yesterday











  • @PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.

    – Stilgar
    yesterday












  • 4





    I believe it is the difference between the compile time evaluation of the expression and the runtime one.

    – meJustAndrew
    yesterday






  • 3





    Isn't 5 / 0 a constant during compilation while i / 0 is not?

    – Christian Ivicevic
    yesterday






  • 3





    Two identical questions in a couple of minutes?

    – Panagiotis Kanavos
    yesterday






  • 1





    @PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D

    – Charles May
    yesterday











  • @PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.

    – Stilgar
    yesterday







4




4





I believe it is the difference between the compile time evaluation of the expression and the runtime one.

– meJustAndrew
yesterday





I believe it is the difference between the compile time evaluation of the expression and the runtime one.

– meJustAndrew
yesterday




3




3





Isn't 5 / 0 a constant during compilation while i / 0 is not?

– Christian Ivicevic
yesterday





Isn't 5 / 0 a constant during compilation while i / 0 is not?

– Christian Ivicevic
yesterday




3




3





Two identical questions in a couple of minutes?

– Panagiotis Kanavos
yesterday





Two identical questions in a couple of minutes?

– Panagiotis Kanavos
yesterday




1




1





@PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D

– Charles May
yesterday





@PanagiotisKanavos. Yea, it too will probably get deleted before the homework has been turned in :D

– Charles May
yesterday













@PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.

– Stilgar
yesterday





@PanagiotisKanavos yes we were discussing the issue outside Stack Overflow and decided to ask separately. My question is now deleted.

– Stilgar
yesterday












4 Answers
4






active

oldest

votes


















13














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.






share|improve this answer


















  • 1





    They could have made it so this case generates code to throw DBZ exception at runtime.

    – Stilgar
    yesterday






  • 5





    This doesn't hold for anyInt / 0 : the compiler could know it's always illegal.

    – Henk Holterman
    yesterday












  • @HenkHolterman in what case is it not true for anyInt / 0?

    – Stilgar
    yesterday






  • 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
    yesterday







  • 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
    yesterday


















7














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.






share|improve this answer




















  • 1





    We know what it does, we are wondering why it was made this way.

    – Stilgar
    yesterday


















2














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.






share|improve this answer

























  • 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
    yesterday


















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;





share|improve this answer





























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    13














    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.






    share|improve this answer


















    • 1





      They could have made it so this case generates code to throw DBZ exception at runtime.

      – Stilgar
      yesterday






    • 5





      This doesn't hold for anyInt / 0 : the compiler could know it's always illegal.

      – Henk Holterman
      yesterday












    • @HenkHolterman in what case is it not true for anyInt / 0?

      – Stilgar
      yesterday






    • 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
      yesterday







    • 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
      yesterday















    13














    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.






    share|improve this answer


















    • 1





      They could have made it so this case generates code to throw DBZ exception at runtime.

      – Stilgar
      yesterday






    • 5





      This doesn't hold for anyInt / 0 : the compiler could know it's always illegal.

      – Henk Holterman
      yesterday












    • @HenkHolterman in what case is it not true for anyInt / 0?

      – Stilgar
      yesterday






    • 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
      yesterday







    • 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
      yesterday













    13












    13








    13







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    Philip CPhilip C

    1,1722143




    1,1722143







    • 1





      They could have made it so this case generates code to throw DBZ exception at runtime.

      – Stilgar
      yesterday






    • 5





      This doesn't hold for anyInt / 0 : the compiler could know it's always illegal.

      – Henk Holterman
      yesterday












    • @HenkHolterman in what case is it not true for anyInt / 0?

      – Stilgar
      yesterday






    • 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
      yesterday







    • 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
      yesterday












    • 1





      They could have made it so this case generates code to throw DBZ exception at runtime.

      – Stilgar
      yesterday






    • 5





      This doesn't hold for anyInt / 0 : the compiler could know it's always illegal.

      – Henk Holterman
      yesterday












    • @HenkHolterman in what case is it not true for anyInt / 0?

      – Stilgar
      yesterday






    • 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
      yesterday







    • 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
      yesterday







    1




    1





    They could have made it so this case generates code to throw DBZ exception at runtime.

    – Stilgar
    yesterday





    They could have made it so this case generates code to throw DBZ exception at runtime.

    – Stilgar
    yesterday




    5




    5





    This doesn't hold for anyInt / 0 : the compiler could know it's always illegal.

    – Henk Holterman
    yesterday






    This doesn't hold for anyInt / 0 : the compiler could know it's always illegal.

    – Henk Holterman
    yesterday














    @HenkHolterman in what case is it not true for anyInt / 0?

    – Stilgar
    yesterday





    @HenkHolterman in what case is it not true for anyInt / 0?

    – Stilgar
    yesterday




    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
    yesterday






    @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
    yesterday





    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
    yesterday





    @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
    yesterday













    7














    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.






    share|improve this answer




















    • 1





      We know what it does, we are wondering why it was made this way.

      – Stilgar
      yesterday















    7














    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.






    share|improve this answer




















    • 1





      We know what it does, we are wondering why it was made this way.

      – Stilgar
      yesterday













    7












    7








    7







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    meJustAndrewmeJustAndrew

    3,15932452




    3,15932452







    • 1





      We know what it does, we are wondering why it was made this way.

      – Stilgar
      yesterday












    • 1





      We know what it does, we are wondering why it was made this way.

      – Stilgar
      yesterday







    1




    1





    We know what it does, we are wondering why it was made this way.

    – Stilgar
    yesterday





    We know what it does, we are wondering why it was made this way.

    – Stilgar
    yesterday











    2














    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.






    share|improve this answer

























    • 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
      yesterday















    2














    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.






    share|improve this answer

























    • 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
      yesterday













    2












    2








    2







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday









    IDarkCoder

    346111




    346111










    answered yesterday









    Piotr StappPiotr Stapp

    14.2k44890




    14.2k44890












    • 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
      yesterday

















    • 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
      yesterday
















    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
    yesterday





    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
    yesterday











    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;





    share|improve this answer



























      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;





      share|improve this answer

























        0












        0








        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;





        share|improve this answer













        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;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        dark_3nergydark_3nergy

        628




        628













            Popular posts from this blog

            Sum ergo cogito? 1 nng

            419 nièngy_Soadمي 19bal1.5o_g

            Queiggey Chernihivv 9NnOo i Zw X QqKk LpB