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.
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
posted 8 months ago
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
posted 8 months ago
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.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database