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:

Checked Exception Example

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 for more such questions.

Related Posts

  • 19 September

    Java try with resources

    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 close all the resources declared within try block. It automatically closes the resources after being used. A resource can be any file or a database […]

  • 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

    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-6001b2c2570ea190250501/] Create InvalidAgeException class as […]

  • 19 October

    Exception handling in java

    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 am going to explain about the another OCAJP exam objective “Differentiate among checked exceptions, unchecked […]

  • 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

    Yes,  we can have try without catch block by using finally block. You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.exit(). Lets understand with the help of example. [crayon-6001b2c257ccc256479205/] When you execute above program, you […]

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.