One of the most difficult parts of programming languages is to know the difference between Error and Exception. I used to get confused a lot about this. So, to help my mates to overcome this problem, I thought of writing a blog on it. After a long time experimenting, I have decided to write this blog in a table fashion(sounds odd, but easy to understand). Here you go.
What is an Exception?
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program.
What is an Error?
Errors are the mistakes or faults in the program that causes the program to behave unexpectedly.
Types of Exception
Exception: Most languages throw an exception at run-time except java. Java provides two types of exceptions. Let's see them.
Checked Exception: Checked exceptions are known to the compiler and must handle at compile time.
Unchecked Exception: These errors are not checked at runtime.
Types of Error
Compile Time Error/ Syntax Error: Common syntactical mistakes in the code.
Run Time Error: Run time errors that are caused by the environment in which the program is running.
Logical Error: Mistakes in the logical flow of the program.
Here comes the Point of Confusion :
- Only confusion is between run-time errors and exceptions.
- An exception is something that interrupts the normal flow of the program.
- Something exceptional has happened within the program which should be handled to run the program uninterruptedly.
- An exception that can be handled even in the compile-time in a language like java while that is not true in the case of run-time error.
- Most of the run-time errors caused by the environment in which the application is running.
- An error must be resolved. You simply cannot ignore them.
- They are caused by the program itself.
- Null reference exception is when the program tries to access the property of the null object.
- You can recover from the exceptions by handling them through try-catch classes.
- It is recommended to handle exceptions in the program.
- Errors are mostly caused by the environment in which the application is running.
- OutOfMemoryError occurs when JVM runs out of memory or StackOverFlow error occurs when stack overflows.
- You cannot recover the program from the error. So, you must resolve the error.
Examples of Exception
Example of Errors
- So, Exception is not a run-time error.
- There is a clear difference between errors and exceptions in a language like java.
- Different languages interpret error differently.
That's a wrap. Share your thoughts in the comment section. Connect with me on Twitter for more awesome content.