Checked vs Unchecked Exceptions

Last modified on October 5th, 2014 by Joe.

Type of exceptions in java are checked exceptions and unchecked exceptions. This classification is based on compile-time checking of exceptions. There is also a classification based on runtime in java. It is not widely known! That is synchronous and asynchronous exceptions. First let us see the java checked exceptions and unchecked exceptions.

Checked Exceptions Vs Unchecked Exceptions in Java

At compile time, the java compiler checks that a program contains handlers for checked exceptions. Java compiler analyzes by which checked exceptions can result from execution of a method or constructor.For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class or its superclasses of that exception.

The class RuntimeException and its subclasses, and the class Error and its subclasses are unchecked exceptions classes. Because the compiler doesn’t forces them to be declared in the throws clause. All the other exception classes that are part of Throwable hierarchy are checked exceptions.

Now let us see a see small discussion on why exceptions are classified as checked exceptions and unchecked exceptions.

Those unchecked exception classes which are the error classes (Error and its subclasses) are exempted from compile-time checking in java because they can occur at many points in the program and recovery from them is difficult or impossible.

Example: OutOfMemoryError

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java, having to declare such exceptions would not aid significantly in establishing the correctness of programs.

Example: NullPointerException

Thrown when an application attempts to use null in a case where an object is required.

So the java compiler doesn’t forces them to be declared in the above two cases.

Synchronous and Asynchronous Exceptions in Java

The interpreter executes the java program sequentially. An exception E can occur relative to a line (L) of program. That is that exception E will always occur at the execution of that line L. This is called Synchronous exception.

An asynchronous exception in java can occur at any point in the execution of a program.

They occur only as a result of:

  1. An invocation of the stop methods of class Thread or ThreadGroup
  2. An internal error in the Java virtual machine

Comments on "Checked vs Unchecked Exceptions"

  1. Henna Mullaji says:

    ‘this’ keyword in java programs is used to make a call to the constructor of the same class.

  2. Goutham says:

    nice explanation..thanks!! keep up the good work.

  3. Joe says:

    Welcome Goutham

  4. PR Chauhan says:

    Very Nice and So much good explanation of the topic.
    So much thanks

  5. Anonymous says:

    welll

  6. manoj says:

    good explanantion really helpful

  7. Sandy says:

    hi,
    I just want to know one thing in exception handling mechanism. FileNotFound is a checked exception.It means we are handling at compile time.But it is caught at run time when program executes. How a JVM will decide which none is Checked exception and Which one is Unchecked exception.

    Can you explain the whole scenario of Exception handling ?
    Thanks
    Sandy

  8. varun says:

    Hi joe,

    Very nice explonation…

    I am very new to java.. can you please suggest me whcih books do i need to refer for java,Servlets and JSPs..

  9. komala says:

    Checked Exceptions are exceptions that are handled by the compliler compile time.But it is caught at run time when program executes. How a JVM will decide which one is Checked exception and Which one is Unchecked exception.

  10. Arun Raut says:

    yesterday i was asked the same question. But i could not answer it. But really this is a nice explanation. And i got the answer.

    Thanx a lot buddy…

  11. Francis Antony says:

    very nice………

  12. vema says:

    excellent explaination i love this site..

  13. deep says:

    plz put some sample example

  14. anand says:

    checked exception are those exception which are declare as already it simple mean if we compile our program then it shows a specified exception …..eg.
    import static java.lang.System.*;
    import static javax.lang.Timer .*;
    using this packade we get the checked exceptipon error .it give CE error

  15. sri says:

    Nice Explanation

  16. Pranjal says:

    Nice Explanation, Keep Up The Good Work – Thanks !!

  17. Rajeev says:

    nice example

  18. naidu says:

    it’s nice. very good. at the same time little bit depth is required

  19. Anonymous says:

    how compiler find in comilation time whether that exception is checked or unchecked… can anyone help me about that??

  20. Anonymous says:

    is not a correct explanation.

  21. Anonymous says:

    Today I understood the diff b/w checked, unchecked exceptions.. good article for beginners.

  22. Muruga G says:

    Excellent explanation. Keep going on…….

  23. sabjeet rai says:

    hi joe,
    i am a java student, i want to know that what
    is java native interface(JNI)….

  24. josh says:

    Checked exceptions are actually incredibly convenient. It kills me when I see other programmers catch the superclass and do something generic when they could do something specific to handle the various exceptions they might get.

    I also am discouraged by .NET code that has try/catch littered at various parts in a huge block of code that more or less act as continue statements in a foreach.

    Java can be a slow and painful beast, but they did at least make it easy to put some love into your exception handling. Much encouraged, and very appreciated to your maintainers when you do!

  25. retheeshmeda says:

    very useful………..thanks

    by
    Meda……

  26. Joe says:

    I also feel very bad when people use exception handling for flow control.

    There is a wrong statement. Java is not slow. If you say so, you might be using jdk1.2 or less. Latest versions compete on par with C with respect to performance.

  27. Anonymous says:

    Thanks for this information. Nice work.

  28. shailesh says:

    Thank you so much……

  29. Pushpendra Singh says:

    Thanks for knowledge about checked and unchecked exception…….

  30. Pushpendra Singh says:

    Thank u very much..

  31. megha says:

    yes its really a good explanation.

  32. […] Like NullPointerException, one exception that is very popular is ClassNotFoundException. At least in your beginner stage you might have got umpteen number of ClassNotFoundException. Java class loader is the culprit that is causing this exception. […]

  33. athul says:

    sir i was inot able to understand it can u explain it in a much more simpler way like i have mmy exam on 17th

  34. varun says:

    its really nice notes

  35. shishir says:

    I am not happy with the diff given between checked ad unchecked exceptions.You should give some examples abt the same and explain.
    I m a c language & faculty following yr blog cont. to learn java. Please do the needful…

  36. vennila says:

    your post are really nice..
    one small suggestion have tabular format comparison for differece description. :)

    Thanks,
    vennila

  37. vipin yadav says:

    sir u need to more describe Synchronous and Asynchronous Exceptions in Java.And Checked Exception.

  38. Anonymous says:

    nice work really its soo nice

  39. Chitrank says:

    Great work joe keep it up it has helped me a lot my java examination questions

  40. vijju says:

    it is too short try make some extra matter in exceptions,present it is useful of me.Thankyou.

  41. Ankur Paliwal says:

    NIce Job and is Very HelpFul to all Of us

  42. karthik says:

    Hai sir,

    How compiler find in compilation time whether that exception is checked or unchecked… can anyone help me about that??

  43. mangesh says:

    import java.util.Scanner;
    class std{

    int mark;

    public void get()
    {
    Scanner a=new Scanner(System.in);
    System.out.println(“Enter the Number:”);
    mark=a.nextInt();
    }
    public void set()
    {
    if(mark=50 && mark<=60)
    System.out.println("Pass class");
    }
    public static void main(String args[])
    {
    new std().get();
    new std().set();
    }
    }

    Explain this program plz the result is not getting perfect

  44. Rafi says:

    hi,

    how jvm will identify,which is one is checked,unchecked exception.and filenotfound exception is a checked exception but this will happen at run time.please clarify my doubt

  45. Menaka says:

    Hi Joe,
    I have a doubt in this…Runtime Exceptions and Erros can’t be caught in compile time so it may fall under Synchronous or Asynchronous exceptions rite???please clarify my doubt..

  46. Kishwar says:

    Hi,
    Can u explain me what is IO Exception??
    and when will we use it??

  47. siva says:

     What happens if we have same mapping in multiple struts-config? Which one gets loaded?

  48. Aritz says:

    Good. Well done blog.

  49. Narayana says:

    Yes it is very useful for me…

    Thanks a lot…

  50. Android Apps says:

    Well explained!!!

  51. Bsp Chand says:

    Very nice explanation!!

  52. Anonymous says:

    Guys please refer the “Java language Specfication ” Book many times for more.

  53. sunil says:

    sir,
    i have a question…sir we know that jvm throw an object on our programe of exception that is expose in our programe..
    But in checked exception the compiler declare exception without the help of jvm..how..??

  54. Anonymous says:

    Good one. It is really helpful to me…..

  55. Anonymous says:

    i like d blog

  56. Anonymous says:

    i like d blog

  57. Sree says:

    Its really nice. Easy to understance. thanks a lot joe..

  58. Anonymous says:

    check the condition if it is not a typing mistake….

    “if (mark==50…)”

  59. harish says:

    i want to know about interfaces….why we really need it?
    means my question is in interfaces we can only define method declaration after that we have to again define in the class for their defination.
    we can do directly this thing why two times writing ?

  60. […] and its subclassess are shown in dark green. RuntimeException and all its sub classes are called un-checked exceptions, since they need not be declared using throws in method signature and all other java exceptions are […]

  61. Praveen says:

    nice,
    very useful…

  62. dimple says:

    realy nice explanatn..
    and in easy lang…
    thank u..

  63. rk says:

    Good One!

  64. Sarabeshwar says:

    Hi varun , You can go for HeadFirst Series for referring and learning Java . And also some good blogs .

  65. Jaiprakash says:

    Can a checked exception be handled without a catch block, using only finally block?
    if yes, why?

  66. Anonymous says:

    ‘this’ is a keyword, that is used to call the particular instance.

  67. Anonymous says:

    this(), this with paranthesis is used to call constructor of the same class…!

  68. Shyam says:

    How to convert unchecked exception to checked one and under what are conditions this can be done as we know spring and hibernate is based on this.

  69. Anonymous says:

    very intersting

  70. arnika says:

    Why do we need to declare a checked exception?

  71. Anonymous says:

    yes

  72. nathan says:

    your quick notes is very nice and is easy to understand. thanks -Senthil

  73. Ayush jha says:

    Checked exception: Exceptions which will force you to handle/declare them before running your code. eg. IOException, SQLException.

    Note: without declaring checked exception u can’t run your code successfully.

    Unchecked exception:Exception which don’t force you to handle/declare them before running your code. eg. ArithmeticException, NullPointerException,

  74. Ayush jha says:

    See methods invoked directly without a catch block can throw an exception. Failure to catch and handle such exceptions results in the abrupt termination of the entire try block.
    Doing such practice may causes any exception thrown in the try block to be lost, preventing any possible recovery method from handling that specific problem.

  75. Ayush jha says:

    without declaring checked exception u can’t run your code successfully:)

  76. vinod says:

    “this” keyword is used to point the current object.

  77. vinod says:

    your quick notes is very nice and is easy to understand.

  78. vinod says:

    Vectors are used instead of arrays, because they expand automatically when new data added to them.

  79. Anonymous says:

    Well said Bro!

  80. Charan says:

    Though RuntimeException extends Exception class, how RuntimeException got this special behaviour(Un checked). I checked both classes. But, there is no difference. RuntimeException constructors simply calls super().
    Who is handling this checked and unchecked behaviour. Will it be handling out of these two classes?
    Does JVM itself checks whether it is subclass of RuntimeException or Exception?

  81. Anonymous says:

    ok

  82. abdul hafeez kalekar says:

    Ok

  83. […] subclass from using this class’s clone() method to obtain a copy overwrite it and throw an exception of type  […]

Comments are closed for "Checked vs Unchecked Exceptions".