Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

page 273 of Implementing Interfaces in Java OCA 8 Programmer I Study Guide (Sybex)

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In page 273 (2nd para)of the sybex guide it says "The compiler would throw an exception, if you define an interface or abstract class that inherits from two conflicting interfaces".But it gives a compiler error as shown in the code snippets. So shouldnt this be like "it gives a compiler error if you define an interface or....." .
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:"The compiler would throw an exception, if you define an interface or abstract class that inherits from two conflicting interfaces". But it gives a compiler error as shown in the code snippets. So shouldnt this be like "it gives a compiler error if you define an interface or....." .

"The compiler throws an exception" is equivalent with "The compiler gives a compiler error".

That statement might be a little bit confusing, but you should remember one very, very, very important rule: the compiler doesn't execute any code at all! So the compiler can never throw a runtime exception. So if the compiler throws an exception, it's definitely a compiler error (and thus compilation fails).

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The compiler throws an exception" is equivalent with "The compiler gives a compiler error"


I find these statements a little contradicting. But okay , I agree with you.
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure If my understanding is right.

If there are no errors in your source file, the Java compiler will produce .class files (converting it to byte code).Once you have successfully compiled your Java source code, you can invoke the Java VM to run your application's byte-code. Then when you run and if there are some issues ... an exception is thrown.

But I saw JLS https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html#jls-11.2.

So a compiler does a Compile time checking of Exceptions..and exempts unchecked exceptions.does it really throw an exception . or am I missing something
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:I find these statements a little contradicting.

I agree with you! I would never use the statement "the compiler throws an exception" myself, because that could be very confusing (as this topic illustrates). I would use "the compiler gives a compiler error" or "compilation fails when you try to compile this code" or ...

But if you keep in miind that the compiler never executes any code, the compiler can never throw an exception while running the code. So if the compiler throws an exception, it's always at compile time and thus it's always a compiler error.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:If there are no errors in your source file, the Java compiler will produce .class files (converting it to byte code).Once you have successfully compiled your Java source code, you can invoke the Java VM to run your application's byte-code. Then when you run and if there are some issues ... an exception is thrown.

You are spot-on! When your source files are compiled, the compiler checks all your source code and verifies your code against a gigantic set of syntax rules (e.g. don't invoke an unexisting method). If your code violates any of these rules, you'll get a compiler error and compilation fails (so no .class file is created). For example, this code doesn't compile because you can't assign a String to an IntegerWhen all your code is valid syntax (and thus doesn't violate any of the syntax rules), the compiler creates a .class file (containing the byte code). When this byte code (.class file) is executed, the execution terminates normally or an exception can be thrown at runtime. For example, these code snippets will successfully compile but throw an exception at runtime

Ramya Subraamanian wrote:So a compiler does a Compile time checking of Exceptions..and exempts unchecked exceptions.does it really throw an exception . or am I missing something

In Java, you have 3 kinds of exceptions: checked exception, runtime exceptions and Errors. And only the checked exceptions are verified by the compiler (hence their name "checked exceptions"). One of the rules for checked exceptions is: you must handle or declare a checked exception. So if you don't handle nor declare a checked exception, the compilation will fail and you'll get a compiler error. For example, IOException is a checked exception and therefore this code snippet will not compile (and you'll get a compiler error)But if you handle or declare this exception, the compiler is happy and the code will successfully compile (without compiler errors)

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay..I got it now. Thank you Roel !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic