Exception Handling in Java

× "Is a" and "Has a" relation "Has a" relation Arrays as in memory Two Dimensional Arrays Exception Handling Variable, Object & Reference Multi-threading Website Design / Development Story of JEE modules
× JAVA, JEE, SQL: Online Private Tutoring Project based Training Concept based Training Basic Java:           $180 Advanced Java:     $300
(Threads,Sockets,JDBC,GUI)
Advanced Java:     $300
(with games projects like
PcMan, PingPong, TicTacToe)
Author: Bharat Chhajer

Overview

Exception Handling in Java is just like how it is in real life. Lets say you are driving from Princeton to New York.

  1. How would you handle the case where in your car meets with an accident.
  2. Can you do anything to avoid break failure.
  3. Can you do anything if an earth quake occurs on your way to New York ?

These are the three kinds of situation you come across in any program.

So, How do we handle exceptions ? How do you program your code with good exception handling ? This is the question that comes to any one's mind who has just learnt OOPS concepts in Java and is looking to learn more.

Exceptions are kind of thrown when they occur. If you catch them and and do something about it, the program continues smoothly with what it was doing. If you don't catch them, the program breaks and stops and you see red lines in the output. So, why would you not catch any exception ? Why would you allow it to break and stop a program. The reason is that there are kinds of exception which you are not supposed to caught. Confusing, right ? Read on....

Types of Exceptions:

Diagram explaining Java Exception Handling.

There are two types of exceptions in Java. Checked Exception and Unchecked Exception. In other words, Compile time Exception and Runtime Exception. Yes, I am giving the other terminology because otherwise you will tell me that you have heard of them and what are those. There is double terminology for almost everything in Java. This makes Java look difficult, when its not actually easy. You might still be feeling that Java is difficult, never mind, lets continue on with Exception handling, so that I can show you that Exception Handling is easy.

Checked and Unchecked Exceptions:

The only two types of exceptions in Java are Checked and Unchecked. There is a class in Java called Exception, whose parent class(super class) is called Throwable. Throwable has two children, Exception and Error. Both Exceptions and Errors can be thrown literally. This is because they are children of Throwable and inherit that quality from it. We will talk about Errors later. We will continue on with Exceptions.

Class Exception has a lot of children classes(subclass). Exceptions are of two types, Checked and Unchecked Exception. So, how do you know which are checked and which are unchecked ? Class Exception has a child class called Runtime Exception. All its children and their children and so on... are Unchecked Exceptions. All the brothers/sisters of Runtime Exception and their families under them are Checked Exceptions. In other words, all the children(and families under them) of Exception class except Runtime Exception are Checked Exceptions. They are called Checked because compiler checks for them and forces you to decide on weather you want to catch and handle it or not (allow it to break and stop your program).

Example FileNotFound Exception. Unchecked exceptions should never if handled, so the compiler does not force you to decide on its handling. If you find an unchecked exception during testing you are supposed to fix your program so that an unchecked exception never occurs. Example NullPointer Exception. They are like break failure. So when you find that break has failed during testing, go and fix it. You don't handle a break failure when it happens while running the Car. It should be fixed when found, so that it never happens upon driving.

Errors are like Earth Quake, like they happen due to a problem in JVM or physical machine. If they occur, they occur. You cannot do anything about them, accept that you report it to JVM creator or System Administrator.

Exception Handling Strategy:

For the Checked Exceptions, the compiler will force you to do one of the two things. Either do a try catch finally or mention the throws clause at the end of method signature. For the unchecked exception, you have to fix them so that they never occur. For the Errors you cannot do anything. The Error will get displayed on the screen when program fails due to JVM problem or machine problem.

Summary:

In Java, Exceptions are either Checked Exceptions or Unchecked Exceptions. Checked Exceptions are checked by the Compiler and the compiler forces you to decide on weather you want to handle it or not. Unchecked Exception are not supposed to be handled. They should be fixed when found to occur in certain conditions in a program. I have not gone into examples as you will find the examples every where on the net. My attempt was to explain the exceptions in its totality and in a easily understandable way.

Note: If anything is confusing in this article you can email info@ javasprint.com . This article will get updated. Learn and help learn.

Author: Bharat Chhajer

Important definitions

What is Java? - Definition

A high-level programming language developed by Sun Microsystem. By now, owned by Oracle. Java was originally called OAK, and was designed for handheld devices. It became popular in web programing since around 1995. Now, its popular with cell phones, televisions, gaming devices, almost every device type. Java is a Object oriented language like "C++" minus some error prone features like pointers and multiple inheritance from Classes.

What is inheritance? - Definition

Inheritance is a concept in which a sub class(child) inherits fields and methods of a super class(parent). Inheritance establishes a "Is A" relation between classes. Example: Student "is a" Person. Teacher "is a" Person.

What is polymorphism? - Definition

Polymorphism is where two methods have same name but different behaviour. Its of two types, Over ridding and Over loading.

What is encapsulation? - Definition

Encapsulation is hidding information with in an Object

What is association? - Definition

Association is a concept that enables relating one Object with another with a "Has A" relation. Example: Car "has a" wheel.