Handling Exceptions in Java Programming
Previous | Home | Next |
Actually exceptions are used for handling errors in programs that occurs during the program execution.
If any error occurs during the program execution and you want to print your own message or the system message about the error.then exception handling is used.
Exception handling consists two blocks
Try Block:-you write the part of the program which generate the error in the try{}block
Catch block:-this block is used to catch the errors.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
Syntax of Exception handlingtry{
/*
Contains code to be tested
*/
}
catch(Exception e)
/*
Contains code to be executed if instanced of Exception is caught
*/
}
import java.io.*;
public class ExceptionHandling{
public static void main(String[] args) throws Exception{
try{
int x,y;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
x= Integer.parseInt(in.readLine());
y = Integer.parseInt(in.readLine());
}
catch(NumberFormatException ex){
System.out.println(ex.getMessage() + " is not a numeric value.");
System.exit(0);
}
}
}
Previous | Home | Next |