Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between errors and unchecked exceptions in java?

As we know if any error or any unchecked exception occurs then our program will halt, then what are the differences between those?

like image 607
GuruKulki Avatar asked Apr 22 '10 18:04

GuruKulki


1 Answers

Unchecked Exception:

  • The classes that extend RuntimeException are known as unchecked exceptions
  • Unchecked exceptions are not checked at compile-time rather they are checked at runtime.And thats why they are also called "Runtime Exception"
  • They are also programmatically recoverable problems but unlike checked exception they are caused by faults in code flow or configuration.
  • Example: ArithmeticException,NullPointerException, ArrayIndexOutOfBoundsException etc
  • Since they are programming error, they can be avoided by nicely/wisely coding. For example "dividing by zero" occurs 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 techniques

Error:

  • Error refers irrecoverable situation that are not being handled by try/catch

  • Example: OutOfMemoryError, VirtualMachineError, AssertionError etc.

    This question may also be helpful in this context - Runtime/Checked/Unchecked/Error-Exception

like image 63
Razib Avatar answered Oct 14 '22 04:10

Razib