What is difference between checked and unchecked exception ?

Showing Answers 1 - 18 of 18 Answers

psuresh1982

  • Oct 25th, 2007
 

checked Exceptions must be dealt with in either a try/catch block or by declaring a "throws" in a method. Unchecked exceptions normally are Runtime exceptions like NullPointerException or ClassCastException.

A simple rule of thumb: If it's an exception you can possibly deal with (continue to run the program using some alternative code), use checked exceptions. For exceptions that should never happen (if they do, it's a bug), use unchecked (Runtime) exceptions which will come up to the surface and displayed to the user. Like this you assure that if there's a bug, it will show up eventually and can be fixed, and you don't run the risk of catching an exception and forgetting to deal with it (f.i.
empty catch block).

Also if you want more details then go through the following URL....

http://www.javapractices.com/Topic129.cjp

sampra

  • Feb 14th, 2008
 

checked exp is complie time exception or caught excep eg aerithmatic exp
unchecked is runtim excption or uncaught excep  eg virtual memory

  Was this answer useful?  Yes

checked exceptions must be handeled.That is, a try-catch must either be nested around the call to the method that throws the exception.The compiler will throw an error message if it detects an uncaught exception and will not compile the file.

The run-time exceptions(unchecked exceptions) do not have to be caught. This avoids requiring that a try-catch be place around, for example, every integer division operation to catch a divide by zero or around every array variable to watch for indices going out of bounds.

You can use multiple catch clauses to catch the different kinds of exceptions that code can throw as shown in this snippet:

try
  {
   
... some code...
  }
  catch (ArrayIndexOutOfBoundException e)
  {
   
...
  }
  catch (IOException e)
  {
   
...
  }
  catch (Exception e)
 
{
   
...
  }
  finally
// optional
  {
   
...this code always executed even if
       no exceptions...
  }

 


  Was this answer useful?  Yes

inderjeet singh

  • May 12th, 2012
 

Checked Exception represents the External Failure while Unchecked Exception represents the Internal failure.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions