Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the difference between a compilation error and an exception please?  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between a compilation error and an exception, please?
 
Saloon Keeper
Posts: 8740
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A compilation error occurs when you try to compile invalid code. An exception occurs when you run your compiled program, but it runs into problems.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compilation error: Compilation is "not" successful, so the code cannot be run.

Exception: Compilation is successful, so the code can be run. However, it might have some problems regarding program flow (Usually logic problems) and those are called exceptions. You can use try-catch to handle the program's behavior when an exception has been encountered.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compilation Error
Encountered when Java compiler cannot compile your code often because of bad syntax in the program ( I said often to make things simple but can't generalize).

Exception
Your program will pass compilation without any issues, but at some point in run time, program crashes due to poor logic. Eg:- Attempting to divide by zero, Accessing illegal array indexes
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you guys have said is the obvious. I am after a more detailed explanation with reference to unchecked and checked exceptions to be specific. Thanks in advance.
 
Stephan van Hulst
Saloon Keeper
Posts: 8740
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between checked and unchecked exceptions is completely unrelated to your original question. ALL exceptions, whether checked or unchecked, happen at runtime. Compilation errors are related to neither.

Checked exceptions are those which can happen because of circumstances outside of the programmers control. For instance, you might be in the middle of a file transmission when a rat gnaws through your Ethernet cable. Because you should be ready for these kinds of problems, IOException is a checked exception. The compiler will warn you when you don't deal with a situation that could lead to a checked exception at runtime.

Unchecked exceptions happen because the programmer made a mistake. When they occur, that's considered a bug in the code, and the programmer should fix the mistake they made. The compiler doesn't check for these situations because it assumes that the programmer has read and understood the contracts of the methods they use, and that they use them accordingly.

Checked exceptions are all those exceptions that derive from Exception, but not those that derive from RuntimeException. Note that RuntimeException is a really poor name, because all exceptions occur at runtime. This is likely the source of the confusion in your original question. Instead, the designers should have called it UncheckedException.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!