In Java Exception Handling is managed by 5 key word,they are
- try
- catch
- finally
- throw
- throws
Here i am posting a typical example code which will make use of these five keywords,in this example we are writing code for preparing Tea.
For preparing the tea we require the following Ingredients
- TeaPowder
- Milk
- Sugar
If these things are not available at the preparation time we are throw Exception objects created from our Exception classes like
- SugarException
- TeaPowderException
- MilkException.
As we know to create our own exception classes we have to extend it from the Base Class Exception.
The code for this is shown below
import java.io.DataInputStream;
import java.io.IOException;
/*
* MakeTea.java
*
* Created on November 16, 2010, 9:11 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author NVAEEN S MIRAJKAR
*/
class SugarException extends Exception
{
int errorCode;
String info;
public SugarException()
{
this.errorCode=100;
this.info="No Sugar Exception";
}
public String getDetails()
{
return("Error Code="+this.errorCode+"\t"+this.info);
}
}
class MilkException extends Exception
{
int errorCode;
String info;
public MilkException()
{
this.errorCode=101;
this.info="No Milk Exception";
}
public String getDetails()
{
return("Error Code="+this.errorCode+"\t"+this.info);
}
}
class TeaPowderException extends Exception
{
int errorCode;
String info;
public TeaPowderException()
{
this.errorCode=102;
this.info="No Tea Powder Exception";
}
public String getDetails()
{
return("Error Code="+this.errorCode+"\t"+this.info);
}
}
public class MakeTea {
/** Creates a new instance of MakeTea */
int milk;
int teaPowder;
int sugar;
public MakeTea(int sugar,int teaPowder,int milk) throws SugarException,MilkException,TeaPowderException
{
if(milk==0)
{
MilkException mke=new MilkException();
throw mke;
}
if(sugar==0)
{
throw new SugarException();
}
if(teaPowder==0)
{
TeaPowderException tpe=new TeaPowderException();
throw tpe;
}
this.milk=milk;
this.teaPowder=teaPowder;
this.sugar=sugar;
}
public static void printScreen(String msg)
{
System.out.println(msg);
}
public static void main(String args[])
{
int sugar = 0;
int milk = 0;
int tpowder = 0;
MakeTea.printScreen("To Prepare Tea First on the gas an lite the stove");
try
{
DataInputStream din=new DataInputStream(System.in);
MakeTea.printScreen("Enter the quantity of Sugar");
sugar=Integer.parseInt(din.readLine());
MakeTea.printScreen("Enter the quantity of Tea Powder");
tpowder=Integer.parseInt(din.readLine());
MakeTea.printScreen("Enter the quantity of Milk");
milk=Integer.parseInt(din.readLine());
MakeTea.printScreen("The Quantities entered are \n"+
"Sugar="+sugar+
"\nTeapowder="+tpowder+
"\nMilk="+milk);
}
catch (IOException ex)
{
MakeTea.printScreen("Cannot read values from key board");
}
try
{
MakeTea mkt=new MakeTea(sugar,tpowder,milk);
MakeTea.printScreen("We have Sucessfullt prepared the Tea now enjoy......!");
}
catch (SugarException ex)
{
MakeTea.printScreen(ex.getDetails());
}
catch (MilkException ex)
{
MakeTea.printScreen(ex.getDetails());
}
catch (TeaPowderException ex)
{
MakeTea.printScreen(ex.getDetails());
}
finally
{
MakeTea.printScreen("Wether u are able to make tea or not OFF the Stove n GAS");
MakeTea.printScreen("Gas n Stove Turned OFF");
}
}
}
No comments:
Post a Comment