Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Java Exception - Exception Handling in Java

 
 

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

Advertisements

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

Discuss: Java Exception - Exception Handling in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:27
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