To generate Exception

23/11/2009 17:19

/*******************************************************************
Program name:Write a java program to generate exception
Roll no:16
*******************************************************************/
import java.lang.*;
import java.lang.Exception;
class MyException extends Exception
{
MyException(String Message)
{
super(Message);
}
}
class TestMyException
{
public static void main(String args[])
{
int x=5,y=1000;
try
{
float z=(float)x/(float)y;
if(z<0.01)
{
throw new MyException("No. is too small");
}
}
catch(MyException e)
{
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("I am always here");
}
}
}

Back

Search site

Copyright © 2012 Dadaso Zanzane. All rights reserved.