• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Doesn't compile" vs "an exception is thrown"

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I was just wondering, when an exception is thrown does it effectively mean that, other than the fact that an exception gets thrown of course, the code doesn't compile too? There are questions whose answers are, among the other ones, Doesn't compile, an exception is thrown, and I' thinking that if an exception is thrown the code is effectively not compiling...
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions are always thrown at runtime, meaning the code must have compiled in order to run.

A lot of confusion comes from the term "runtime exception", which was a bad way to name them, because all exceptions are thrown at runtime. Instead, they should have been called "unchecked exceptions".

A compilation error is not an exception.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thanks for clarifying it. I guess when you try to compile an application from the console and it throws an exception I wasn't sure whether it would compile it first or because it threw an exception it would effectively not compile because of the exception. It's clear now though!
 
Liutauras Vilda
Bartender
Pie
Posts: 2803
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:I guess when you try to compile an application from the console and it throws an exception I wasn't sure whether it would compile it first or because it threw an exception it would effectively not compile because of the exception.

Just to clarify once again.

Code either compiles or not, it has no direct connection with exceptions being thrown.

Now about exceptions. As Stephan mentioned, all exceptions being thrown at run time, always, in simple words, when the application is running. However, some possible exceptions need to be handled before you run your application, because you can foresee as a developer that they might occur at run time, and that handling part you're being forced by the compiler to provide before you run your code. Other type of exceptions you cannot predict if they will be thrown or not, but important part is, that if they are thrown, they are being thrown because of developer's overlook, having said that, it means developer did something wrong in the coding part.

So, the ones which you can foresee as a developer and require to have appropriate handling code blocks before you run your code, are called - checked exceptions.
The other exceptions, which happen because of programmers overlook, are called - unchecked exceptions.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make this really explicit:

If the error message comes after you type the javac command, it doesn't compile. If the error message comes after you type the java command, an exception it thrown.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the error message comes after you type the javac command, it doesn't compile. If the error message comes after you type the java command, an exception it thrown.

that is the way I understood it, and I reckon this is exactly what confused me. So if the error message comes after I type the javac command, it's not an exception but an error?
 
Liutauras Vilda
Bartender
Pie
Posts: 2803
112
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:So if the error message comes after I type the javac command, it's not an exception but an error?

Compilation error. Compilation word is important as it clarifies that you're talking about syntax errors, because there is in Java such class Error which is a subclass of Throwable and it is being thrown when something serious happen as system runs out of memory, hard drive dies, or some other serious abnormal things.
More read here and here.
 
Henry Wong
author
Marshal
Pie
Posts: 22122
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Unfortunately, with the capability that many IDE provides, ie. the ability to run programs that do not compile, it throws a monkey wrench in this discussion. So, let's pretend that IDEs don't have this feature -- and definitely, do not use this feature. See this for reasons why...

https://coderanch.com/wiki/660183/Wiki/Fix-Compiler-Errors-Running-Application

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic