Your Only Destination To Empower Your Computer Programing Knowledge. Sky is not so high, You can Touch it if You Try

Program to create your own exception class

import java.lang.Exception;
import java.util.*;
class MyExp extends Exception
{
MyExp(String message)
{
super(message);
}
}
class My {

public static void main(String ar[])
{
System.out.println("Enter value of x and y"); Scanner a=new Scanner(System.in);
float x=a.nextFloat();
float y=a.nextFloat();
try
{
float z=x/y;
if(z<0.1)
{
throw new MyExp("Number is too small");
}
}
catch(MyExp e)
{
System.out.println("My Exception is caught");
System.out.println(e.getMessage());
}
finally

{
System.out.println("Hello mca");
}
}
}


Output:-


C:\Documents and Settings\Administrator>cd\

C:\>cd\java\bin

C:\java\bin>javac My.java

C:\java\bin>java My
Enter value of x and y
1.0
11.0
My Exception is caught
Number is too small
Hello mca

C:\java\bin>

No comments:

Post a Comment