• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 10104
212
  • 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: 12
  • 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: 10104
212
  • 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.
 
Evil is afoot. But this tiny ad is just an ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!