Page 1 of 1

[HD Pg 23, Sec. 1.8.3 - compilation-error-vs-exception-at-run-time]

Posted: Wed Apr 24, 2024 4:37 am
by mcpunjabi
What??!

For example, the statement byte b = 200; is syntactically correct but the compiler does not like it. The compiler knows
that the value 200 is too big to fit into a byte and it believes that the programmer is
making a logical mistake here.


You say compiler correct only syntactical errors not logical as JVM does.

Then you say that byte b = 200; is a logical error but IT IS NOT BECAUSE IT IS A SYNTACTICAL ERROR

Re: [HD Pg 23, Sec. 1.8.3 - compilation-error-vs-exception-at-run-time]

Posted: Wed Apr 24, 2024 7:01 am
by admin
The complete paragraph prior to this line of code that you are referring to is this:
Besides being syntactically correct, the compiler wants to make sure that the code is
logically correct as well. However, the compiler is limited by the fact that it cannot
execute any code and so, it can never identify all the logical errors that the code may
have. Even so, if, based on the information present in the code, the compiler determines
that something is patently wrong with the code, it raises an error. It is this category of
errors that causes the most frustration among beginners.
So, the text quite clearly explains the issues involved. Compiler prevents all syntactical errors and also does prevent some logical ones (but not all logical errors, because it can't detect all of them).

So where is the confusion??

Re: [HD Pg 23, Sec. 1.8.3 - compilation-error-vs-exception-at-run-time]

Posted: Thu Apr 25, 2024 3:09 am
by mcpunjabi
Compiler does prevent some logical ones but not all logical errors?

Can you give me 1 example of logical error the compiler prevent and 1 example of logical error the compiler doesnt prevent?

Re: [HD Pg 23, Sec. 1.8.3 - compilation-error-vs-exception-at-run-time]

Posted: Thu Apr 25, 2024 3:26 am
by admin
Both are already given in the same paragraph:

byte b = 200; has a logical issue. There is nothing wrong with it syntactically. The value 200 cannot fit into a byte. The compiler notices that and refuses to compile.

int i = 10/0; also has a logical issue because an int datatype cannot represent infinity. But the compiler does not notice it and accepts the code as valid.

Re: [HD Pg 23, Sec. 1.8.3 - compilation-error-vs-exception-at-run-time]

Posted: Thu Apr 25, 2024 3:45 am
by mcpunjabi
ok thank you