Exception v/s Error
Exception
|
Can be handled.
|
Error
|
No solutions to a problem.
|
Error
Examples:
- Memory failure (RAM).
- Accessing unavailble data.
- Power failure error.
Exception Handling
Examples:
- Something which is checked (or already known) to handle that situation.
Can be handled by self & so is checked.
- Unchecked + Errors
Checked, but cannot be handled by self.
Compile - Time Exception
a.k.a. “Checked Exception”
Compiler / Interpreter knows. Also, the programmer knows how to handle it.
- Using variable without declaring it.
- Trying to change/re-assign
const
identifier.
- Syntax error.
- Treating local variable as a global variable.
Runtime Exception
a.k.a. “Unchecked Exception”
- Index out of bound exeption.
- Condition not terminated.
- Arithmetic exception.
- Using freed memory.
- De-refrencing NULL or dangling pointer.
- Memory leak.
- Uncompatile exception.
- ENF / DNF.
- Class not found.
These are unchecked but still resolved only by the programmer’s help. Only some exceptions can be handled with the help of compiler.
5 Keywords
There are 5 keywords for exception handling in most programming languages.
try
catch
throw
throws
finally
We cannot try
without catch
.
We cannot catch
without try
.
Exception Chaining
- Some exceptions are useful to us in a particular situation / time.
- So that paricular exception can be handled at a particular & desired situation & time.
- Thus, we chain exceptions up.