• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the difference between "compilation fails" and "An exception is thrown at runtime"

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading the K&B book and find some questions with these two options. I get confused about the difference between this two concepts. Anyone can tell me the difference and how this situation also would happen in exam...
 
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
Hi nemo zou,

First of all, a warm welcome to CodeRanch!

nemo zou wrote:I'm reading the K&B book and find some questions with these two options. I get confused about the difference between this two concepts. Anyone can tell me the difference and how this situation also would happen in exam...

The compiler is not involved when the code is ran. That's why you have javac (the java compiler) to compile your classes and java to run your classes. The compiler makes sure you don't do things which are forbidden (like trying to use a non-existing variable, use incorrect java syntax, assign a long to an int). If you do something forbidden, you'll get a compiler error (and no class file). So the compiler does NOT care what happens at runtime, it's only interested in what it knows at compile time. If you do not have a class file, you can not run the program. So first of all your code should be valid and have no compiler errors at all.
Then you can compile your code (using javac) and you'll get a .class file which you can run using java. When something goes wrong while running the application (and which was not handled), you'll get a runtime exception. For example: division by zero, invoking a method on a null reference, accessing an array (or list) using an invalid index,...

Some compiler errors:
And some runtime exceptions:

On the exam almost every question will have "compilation fails" and "An exception is thrown at runtime" options. So you definitely have to able to determine if code compiles successfully and if exceptions will occur at runtime.

Hope it helps!
Kind regards,
Roel
 
nemo zou
Ranch Hand
Posts: 35
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your detailed explanation. I found this also may be useful.
http://stackoverflow.com/questions/846103/runtime-vs-compile-time
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:I found this also may be useful.
http://stackoverflow.com/questions/846103/runtime-vs-compile-time

Although it's not specific about Java, it's indeed also a useful resource.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic