This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the program.
Checked Exception:
Throwable
class except RuntimeException
and Error
are known as checked exceptions. try/catch
or throw
the exception) then a compilation error occurred. try/catch
block.IOException
, SQLException
etc Unchecked Exception:
RuntimeException
are known as unchecked exceptions ArithmeticException
,NullPointerException
, ArrayIndexOutOfBoundsException
etc ArithmeticEceeption
. We can avoid them by a simple if condition - if(divisor!=0)
. Similarly we can avoid NullPointerException
by simply checking the references - if(object!=null)
or using even better techniquesError:
Error
refers irrecoverable situation that are not being handled by try/catch OutOfMemoryError
, VirtualMachineError
, AssertionError
etc.