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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
mcpunjabi
Posts: 6
Joined: Tue Apr 23, 2024 3:03 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10074
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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??
If you like our products and services, please help us by posting your review here.

mcpunjabi
Posts: 6
Joined: Tue Apr 23, 2024 3:03 am
Contact:

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

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

admin
Site Admin
Posts: 10074
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.


Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests