Java/J2EE and SOA (305 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (304 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What are Checked and Unchecked Exceptions in Java?

Published on Jul 31,2019 23 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Exceptions are an integral part of any programming language. In this post, we will explore exceptions and deeply understand the concept of Checked and Unchecked Exceptions in Java. So, follow along with me we have a lot to look at and wrap our minds around.

Following pointers will be covered in the article,

Moving on with this article on Checked and Unchecked Exception in java.

 What are Exceptions in Java?

Before understanding the concept of checked and unchecked exceptions, first, let’s understand what are exceptions in general. If your program consists of a code which depends on something – Let’s say you have a method which depends on the parameters entered by the user. The compiler will compile the program with no issues as it is syntactically correct but what if the user enters the input in a format which isn’t compatible with the method you wrote? These are known as exceptions and the programmer can do something about these exceptions in the program as a precaution. If you are situations which you cannot prepare for then it is known as an Error.

 There are 2 types of exceptions

  1. Checked Exceptions
  2. Unchecked Exceptions

Moving on with this article on Checked and Unchecked Exception in java.

Checked Exceptions

Now we exactly know what are exceptions. To understand checked exceptions in its vanilla form, let’s consider an example. Consider you are planning a foreign tour and you made a list of item that you are going to carry with you. Along with the first list, you made one more list which consisted of all the essential things that are to be carried such as tickets, passport, etc before leaving for the airport and you set a reminder in your phone that will remind you about the list of essential items to carry before you leave.

What if for some reason the reminder failed and you forgot to carry the ticket and the passport? In that case, you’ll miss the flight or you’ll have to go home to collect the items and return to the airport. Okay, so the set is ready now for explanation. Now consider your reminder as your compiler and your state at the airport as runtime. The compiler made sure you had all the essential items. in other words, it alerted you about the most common types of mistake done by people before leaving for traveling which may lead to problems at the runtime in our case your situation at the airport. It also makes sure you are prepared if such situations take place.

If you write a program which contains code which may produce common types of exceptions the compiler alerts you about the exceptions that may occur at the runtime and tells you to handle the exception with a try-catch block or throws keyword.

All exceptions belong to the Exception class and Exception class inherits from the Throwable class.

For understanding let’s take a simple example 

class Demo {
public static void main(String[] args)
{
FileReader file = new FileReader("Ourfile.txt");
}
}

Output 

java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

We get the above message when we try to run the program. The message says that the Exception should either be caught using try-catch block or it should be handled using the throws keyword.

Note- A important thing to note here is that checked exception doesn’t occur at compile time. The compiler only alerts us if we are not handling a possible exception that may occur. The checked exception only occurs at runtime.

Moving on with this article, let’s understand unchecked exception in Java.

Unchecked Exception

To understand what are unchecked exceptions, let’s consider the same foreign tour example. So the compiler alerted you about the most commonly occurring exceptions. What if once you reached the airport and you realized your flight is delayed.

The compiler didn’t check if you are prepared to handle the situation if the flight is delayed. Compiler not checking for this situation is pretty logical as it not an exception that is common and even if it occurs you cannot do anything in this situation.

The exceptions which are not checked by the compiler are known as unchecked exceptions.

Let’s take a classic example to understand the concept in detail.

class Demo {

public static void main(String args[]) {
int FirstNumber = 0;
int SecondNumber = 10;
int z = SecondNumber/FirstNumber;
}
}

When we compile the above program we get absolutely no error. The problem starts when we try to run the compiled program.

class Demo {

   public static void main(String args[]) {
       int FirstNumber = 0;
       int SecondNumber = 10;
       int z = SecondNumber/FirstNumber;
   }
}

Output-

Exception in thread “main” java.lang.ArithmeticException: / by zero

at Demo.main(Demo.java:10)

We receive the above exception when we try to run the program hence we can conclude that unchecked exceptions also occur at runtime.

In this post, we learned about different exceptions. To know more about how to handle these exceptions refer to this post. It explains all the concepts related to event handling in a simple and detailed manner.

Thus we have come to an end of this article on ‘Checked and Unchecked Exception in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog “Checked and unchecked exceptions in Java”  and we will get back to you as soon as possible. 

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.