Home Java Beginners Java Exception - Exception Handling in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java Exception - Exception Handling in Java

Advertisement
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{
  try{
 
 int a,b;
  BufferedReader in = 
 
new BufferedReader(new InputStreamReader(System.in));
  a = Integer.parseInt(in.readLine());
  b = Integer.parseInt(in.readLine());
  }
  catch(NumberFormatException ex){
  System.out.println(ex.getMessage() 
  +
 " is not a numeric value.");
  System.exit(
0);
  }
  }
}

Download Exception Handling Example

Advertisement

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

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

Post your Comment


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

want to know what does mean SYSTEM MESSAGE?
mohmmad
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
java

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

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

what to do
john
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
java

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

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

nice example sir
Yallappa
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.
muskan
January 31, 2012
computer

nice
Aruna
February 11, 2012
exception handling in java

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

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

When we use the throw and throws in program
Pushpak kumar singh
April 8, 2012
vb,java,oracle,vb.net,c#

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

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

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

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

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

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........ ;)
sivaks
April 11, 2013
display the out put

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

please explain java project release process.
sowjnaya
October 22, 2013
java

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