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












15
















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






  • 3





    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
















15
















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






  • 3





    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














15












15








15


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 Mar 22 at 12:56









meJustAndrew

3,20732552




3,20732552










asked Mar 22 at 12:38









NickNick

2,2871215




2,2871215




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






  • 3





    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





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

    – meJustAndrew
    Mar 22 at 12:42






  • 3





    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




3




3





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












4 Answers
4






active

oldest

votes


















14














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
    Mar 22 at 12:47






  • 6





    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






  • 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








  • 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



















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
    Mar 22 at 12:46



















3














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
    Mar 22 at 12:58



















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









    14














    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
      Mar 22 at 12:47






    • 6





      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






    • 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








    • 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
















    14














    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
      Mar 22 at 12:47






    • 6





      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






    • 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








    • 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














    14












    14








    14







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






    • 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








    • 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














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






    • 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








    • 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








    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













    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
      Mar 22 at 12:46
















    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
      Mar 22 at 12:46














    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 Mar 22 at 12:53

























    answered Mar 22 at 12:45









    meJustAndrewmeJustAndrew

    3,20732552




    3,20732552








    • 1





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

      – Stilgar
      Mar 22 at 12:46














    • 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











    3














    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
      Mar 22 at 12:58
















    3














    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
      Mar 22 at 12:58














    3












    3








    3







    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 Mar 22 at 13:01









    IDarkCoder

    363111




    363111










    answered Mar 22 at 12:49









    Piotr StappPiotr Stapp

    14.6k44991




    14.6k44991













    • 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





    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











    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 Mar 22 at 13:32









        dark_3nergydark_3nergy

        628




        628















            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]