Exception handling in java


What is an Exception- an Exception means a problem arises during execution of main program.

If an Exception came in between the main program it may disrupt the whole program and it may shut down the program and the flow of main program will be effected therefore one should must take care of An Exception.

Following are the reasons in which an exception may occur

  • It is possible user might have entered wrong data.
  • File not present in the parent directory.
  • Lost the network connection while opening file or download.

Some of these error might cause because of user error or inability of user or other programmer. These are the basic error which causes an Exception in Java.

There are checked exceptions, unchecked exceptions or Runtime Exceptions.

Checked Exceptions are occur during compile time, these exceptions cannot be ignored at the time of compilation the programmer should take care of these type of exceptions.

Unchecked exceptions this type of error occur at the time of execution it’s also called as runtime errors it happen due to programming bugs, logic errors and improper use of an API. This type of exception are ignored at time of compilation.

Exception Hierarchy

All exception are sub types of java.lang.exception class. An Exception class is subclass of throwable class. There is subclass called error which is derived from the Throwable class.

Errors are not normal conditions that happen in case of severe failure these are not handle by java programs. These error are not handled by java programs. These errors are generated to indicate errors generated by run time environment.

Programmer handles an exception

Exception Handling: user will handle Java exception by handling and managed via five keywords: try, catch, throw & finally.

Throw: — An example is, when the code accesses an array location which is not available then Array Index out Of Bounds Exception is thrown. Pretty nice name right and obviously it explains the problem. All the java exception classes are not having any attributes and standard methods, It is a common design. Class name describes what exception it is and the hierarchy forms an organization chart kind of structure using which java exceptions are used. An Exceptions can be thrown by either java run time environment or by the code itself JRE throws exception when java’s rules are violated.

Mostly custom exceptions are created based on business conditions. Programmer can use the Java keyword ‘throw’ to generate an exception. It is done by instantiating the exception class of choice and then thrown. Programmer can throw a custom exception or a predefined Java exception. Programmers can throw an exception to indicate a problem condition. Either java’s predefined exceptions can be used or custom exceptions can be created by extending the already available exceptions.

Try — catch

A catch will have an exception identified and it will catch only that type of exception. Type means the same exception and all its sub classes. There can be multiple catch blocks for a try block.

A thrown exception should be handled. If the program does not handles an exception, then it will be handled by the java run time environment. A block of code where an exception is expected should be surrounded by try — catch block. Try indicates the start of the exception handling block and catch the end of the block.

Following catch a block of code can be written which is the exception handling code block. This is the part the handles the exception. When a Java method is going to throw an exception, to indicate that as part of the method signature ‘throws‘ keyword should be used followed by the exception. It means that the caller of this method should handle the exception given in the throws clause. There can be multiple exceptions declared to be thrown by a method. If the caller of that method does not handles the exception, then it propagates to one level higher in the method call stack to the previous caller and similarly till it reaches base of the method call stack which will be the java’s run time system.


After catch block there can be one more block of code declared as ‘finally‘. Irrespective of whether an exception is thrown or not, the finally block of code will always be executed. Important piece of code that must be executed, even if a program fails belong to this finally block. Example would be closing a database connection, a file handle, etc.

This is the just introductory part of tutorial for more details please visit us at


Feel free to contact us

+91 80876 01850