Home Java Beginners Java Exception - Exception Handling in Java


Share on Google+Share on Google+

Java Exception - Exception Handling in Java

In this section, you will learn to the exception handling.

Java Exception - Exception Handling in Java


Exception, that means exceptional errors. Actually exceptions are used for handling errors in programs that occurs during the program execution. During the program execution if any error occurs and you want to print your own message or the system message about the error then you write the part of the program which generate the error in the try{} block and catch the errors using catch() block. Exception turns the direction of normal flow of the program control and send to the related catch() block. Error that occurs during the program execution generate a specific object which has the information about the errors occurred in the program.

In the following example code you will see that how the exception handling can be done in java program. This example reads two integer numbers for the variables a and b. If you enter any other character except number ( 0 - 9 ) then the error is caught by NumberFormatException object. After that ex.getMessage() prints the information about the error occurring causes.

Code of the program : 

import java.io.*;

public class exceptionHandle{
  public static void main(String[] args) throws Exception{
 int a,b;
  BufferedReader in = 
new BufferedReader(new InputStreamReader(System.in));
  a = Integer.parseInt(in.readLine());
  b = Integer.parseInt(in.readLine());
  catch(NumberFormatException ex){
 " is not a numeric value.");

Download Exception Handling Example


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2007

Related Tutorials

Ask Questions?    Discuss: Java Exception - Exception Handling in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
jyoti paitl
April 11, 2011

want to know what does mean SYSTEM MESSAGE?
May 26, 2011
my expression about your web site

this web very good progarming for java and . net and pobject orinted thank you very much
shah javaid
June 3, 2011

i like the way of understanding that you use
July 8, 2011

i m satisfy but i want brif discose of the java subject
September 25, 2011

what to do
September 30, 2011
how to do this???

write a program named GoTooFar in which you declare an array of five integers and store five values in the array. Initialize a subscript to zero. Write a try block in which you acces each element of the array, subsequently increasing the subscript by one. Create a catch block that catches in eventual ArrayIndexOutOfBoundException, and then print to the screen the message,"Now you're gone too far".
Amjad khan
February 20, 2012

i want make program without main . u know so tel me
October 31, 2011

I have got a good knowledge about exceptions.
January 25, 2013

nice example sir
February 27, 2013

This is very useful thanks to roseindia
how to hanlde all exceptions in one execution at output,which includes Arthematic Exception,Number Format Exception,Negative Arays Size Exception,Arrat Out Bounds Exception,String Index Out of Bounds Exception.
January 5, 2012
exception handle

my dear frnds or sir any one know abt this means plz tell
N.Venkata Divya
January 5, 2012
exception handling

write a program to handle all exceptions in one execution,which includes Arthematic Exception,Number Format Exception,Negative Arays Size Exception,Arrat Out Bounds Exception,String Index Out of Bounds Exception.
January 31, 2012

February 11, 2012
exception handling in java

how many types of exceptions in java view details
February 16, 2012
hi can u send details of java exception handling

shall u send entire details about java
February 19, 2012
java exception

When we use the throw and throws in program
Pushpak kumar singh
April 8, 2012

I want to know more about these languages.
April 17, 2012

i like it it is very best to understood the concepts please improve it and continue the same daily
akil khan
September 6, 2012

java is a pure oop language.
nidhi kourav
September 9, 2012
exception handling

this programme occur a error in public keywords?????
September 15, 2012
exception handling

i wanna get solve my exception handling problem to others
September 22, 2012

what different between cpp java
kaushal barot
October 4, 2012
exception handling

to provide the best example then i thanks to you for understand how exception is actually are work
Purusharth Jain
October 4, 2012
I am in 10th class and i am studying java

a very gud prog. for explanation........ ;)
April 11, 2013
display the out put

display the output plz
April 16, 2013
Request for Java Project Release Process.

please explain java project release process.
October 22, 2013

Way of explanation is good and satisfactory. I need some more examples can u please give more examples.