Difference between checked and unchecked exception in java

In this post, we will see difference between checked and unchecked exception in java. It is important question regarding exceptional handling.

What is Exception?

Exception is unwanted situation or condition while execution of the program. If you do not handle exception correctly, it may cause program to terminate abnormally.

What is checked exception?

Checked exceptions are those exceptions which are checked at compile. If you do not handle them , you will get compilation error.
Lets understand with the help of example:
if you do not handle checked exception , you will get compilation error as below:

Add leading zeros to Integer on left

so there are two options two solve above compilation error.
Using try and catch block:
you can put error code prone in try block and catch the exception in catch block.

Example:

As you see, we have put error prone code in try block and various exceptions in catch block. Using throws : You can use throws keyword to handle exceptions.
As you can see , we have used throws keyword to handle the exception.

What is unchecked exception?

Unchecked exceptions are those exceptions which are not checked at compile time. Java won’t complain if you do not handle the exception.

Example:

When you run above program, you will get below exception:

Another example:

When you run above program, you will get below exception:

You can go through core java interview questions for beginners and experienced  and Exception handling interview questions for more such questions.

Related Posts

  • 19 September

    Java try with resources

    Table of ContentsOlder approach to close the resourcesJava 7 try with resourcesSyntaxExampleJava 9 Try with resources ImprovementsFinally block with try with resourcesCreate Custom AutoCloseable Code In this post, we will see about Java try with resources Statement. Java try with resources is a feature of Java which was added into Java 7. It helps to […]

  • Exception Handling Interview Questions
    22 February

    Java Exception Handling Interview Questions And Answers

    Exceptional handling is one of the most important topics in core java. Here is list of questions that may be asked on Exceptional handling. Question 1: What is Exception ? Answer: Java doc says “ An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s […]

  • 15 February

    Difference between throw and throws in java

    Table of Contentsthrow:throws: In this tutorial, we are going to see difference between throw and throws in java. throw: throw keyword is used to throw any custom exception or predefine exception. For example: Let’s say you want to throw invalidAgeException when employee age is less than 18. Create a Employee class as below. [crayon-614d42039c205662535711/] Create […]

  • 19 October

    Exception handling in java

    Table of ContentsExceptionsWhat is Exception ?Exceptions hierarchyUnchecked ExceptionsErrorsDifference between checked exception, unchecked exception and errorsConclusionReferences  Exceptions I have started writing about the and how to prepare for the various topics related to OCAJP exams in my blog. In my previous post, I have published few sample mock questions for StringBuilder class. In this post I […]

  • 17 July

    How to create custom exception in java

    In this post, we will see how to create custom exception in java. It is very simple to create custom exception in java. You just need to extends Exception class to create custom exception. Lets understand this with example. You have list of counties and if You have “USA” in list of country, then you […]

  • 19 June

    Can we have try without catch block in java

    Table of ContentsWhat happens when you have return statement in try block:What happens if you have return statement in finally block tooWhat if exception is thrown in try block In this post, we will see about can we have try without catch block in java. Yes,  we can have try without catch block by using […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.