HANDLING USER-DEFINED EXCEPTIONS

SOURCE CODE:

Exception.java

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=100;
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:

Caught my Exception

Number is too small

I am always here
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: