Friday, November 12, 2010

Exception Handling in Java

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:

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 FileNotFoundException. 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 NullPointerException. 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.


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.

Tuesday, June 30, 2009

Why did you read a lot of Java Books ? Path to learning java programming.

A lot of people who come to me for Java Training at Java Sprint (A Java Training Institute) tell me that they have read a lot of books and material on internet and still they could not learn Java. There are only about 45 keywords in Java and very few concepts that are basic. So, it should not be as difficult to learn Java as people feel it to be.

Take the following steps for self Java Training:

1. Firstly, try and get to a point where you understand "The difference between a Class and a Object". For that try some examples where you create few classes and their objects and call methods on objects.

2. Now try to get to the next level, where you understand "Inheritance and Association". These are the only two relations possible between different classes in a java application. These are sometimes also referred as "Is a and Has a" relation respectively.

3. Next try to get to the third level, where you can do "Object Oriented Way of Programming". For this try and understand Polymorphism, Encapsulation, and the keywords: static, final, public, protected, private.

Once you reach this level you would be a lot more confident with Java. And if you had been hating it, now you might start liking it ! Yeah! Happy programming :)

CEO & Expert Java/JEE Trainer
Java Training at
Get a jump start.

Friday, February 6, 2009

Self Java Training is possible, possible and possible. What are the steps ?

Where do I start? Do I need any tools? I mean, How do I setup Java on my machine?

I am reading and reading but nothing is getting into my head !! If that is how you feel, read on. Best way to learn Java, or for that matter any programming language is by writing and running small examples.

Learn how to setup. Then, how to write, compile and run a simple Java Program that prints Hello on the screen or command prompt. You can do this by following one of the following approaches:

  1. Use JDK (Java SE Development Kit) and Notepad.
  2. Use JDK (Java SE Development Kit) and Eclipse software (Free) or any other software that you are comfortable with to type your program.

Approach 1:
1. Download and install latest JDK from
2. Write your program(may be simple hello world) in Notepad and save the file.
3. JDK
has a bin directory that has javac.exe (compiler) which is used to compile the program and java.exe that is used to run the program. Use them to compile and run the program, respectively. If you saved your program as App. java,
Command to compile:
This will App. class which is used to run the program.
Comand to run:
‘java.exe App’
You will see “hello world” in the output or at command prompt.

Approach 2:
1. First, download and install latest JDK from
2. Download and install latest Eclipse software from
3. Create a Java Project in Eclispe.
4. Create a java class under the source folder (src).
5. Run progrfam. If App.Java is the file that contains your program, right click it and Run as Java Application. You will see “hello world” in a window called console.
Note: Eclipse uses javac.exe and java.exe from the JDK installed to compile and run the java program, respectively. It keeps compiling the code automatically.

Sample Program:
File Name:
public class App{
public static void main(String[] args){

This should arm you with 90% of the things you would need to learn Java. Use any Java Book or just use Tutorials at for additional help.

In the next few days, write and understand simple java programs that have one Java class. Try and learn some of the about 45 keywords that Java language has.

Now, try and write multiple Class programs. After some time you might want to use some of the classes already available in the java library. This library is already installed when you installed JDK. But to know about the Classes available you would need documentation to that library which is popularly known as JAVA API. Google for "JAVA API" to find the documentation online (Its made available at

To be more effective try and follow the following path of topics:

Java Fundamentals
  • Primitive types, Statements and Arrays.
  • Method calls, method input output, Flow of Control
  • Classes, Objects, fields, constructors, methods, package and import
  • Scope of Variables: local variable, object variable, input parameter
  • Modifiers: private, default, protected, public, final and static
  • Command prompt: compile, run, jar, use String args[]
  • Console Input/Output: Scanner, Understanding System.out.println()

Object Oriented Program Design
  • Inheritance, Encapsulation and Polymorphism (Overriding and overloading)
  • Constructors: default and overloaded constructors, super and this
  • Interfaces and abstract classes
  • Relations between Objects: "Has a" and "Is a"
  • Exceptions
  • Usage of collections: ArrayList, HashMap, Iterator, LinkedList

At this point you know Object Oriented way of programming.

Now learn important parts of the Java API:

  • File IO and Serialization
  • Sockets
  • Threads
  • JDBC

After covering the above topics, you would be in quite a good position to dive on to the server side of the java programming. Install a free web server like Tomcat and write your first JSP and first Servlet with help from some tutorial, some book, tutor, a Java Training Course or any other source you can think of.

The above is for self learning. It may take time. But if you are in a hurry, you might need help from a Java Tutor or might want to take up a Java Training Course. Java Training can speed up the travel down this path. It can give a deeper and wider knowledge of the core java concepts and important library/API in a shorter time. Java Training can bring some discipline, timeliness and focut to the Java learning. Java Training in core java can quickly bring you to a step where you can then learn server side Java. Weather you learn yourself or receive Java Training, your drive to learn and the instructor’s drive to teach matter a lot more than anything else.

CEO & Expert Java/JEE Trainer
Java Training at
Get a jump start.