Java Programs
Program for Throwing own exception
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(“Number is small”);
}
}
catch (MyException e)
{
System.out.println(“my exception”);
System.out.println(e.getMessage( ));
}
finally
{
System.out.println(“ always here”);
}
}
}
Program for Throwing own exception
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(“Number is small”);
}
}
catch (MyException e)
{
System.out.println(“my exception”);
System.out.println(e.getMessage( ));
}
finally
{
System.out.println(“ always here”);
}
}
}