Exno: EXCEPTION HANDLING
Aim:
To write a java program to implement the concept of exception handling.
Algorithm:
Step1: Start.
Step2: Create a class MyException which extends the Exception class.
Step3: Define a user defined Exception.
Step4: In void main(), peform an arithmetic operation.
Step5: In try block check the result is less than 0.01.
Step6: In catch block catch the exception and call the user defined exception.
Step7: In finally block display the message.
Step8: Stop.
Program:
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 too small");
}
}
catch (MyException e)
{
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("i am always here");
}
}
}
Output:
Z:\anitha>javac TestMyException.java
Z:\anitha>java TestMyException
Caught my exception
number is too small
i am always here
0 comments:
Post a Comment