User defined Exception

Exceptions can be set in motion explicitly by the user with throws statement. The throws statement has following format:

throws ExceptionOnject;

The ExceptionObject is an object of the class that extends Exception class.

Example:

class InvalidBalance extends Exception
{
InvalidBalance(String msg)
{
super(msg);
}
}
class Example4
{
public static void main(String args[])
{
float f=Float.parseFloat(args[0])
{
try
{
if(f<500)
throw new InvalidBalance("Balance is not sufficient in order to open account");
}
catch(InvalidBalance e)
{
System.out.println(e.getMessage());
}
}
}

Example

class UnnamedException extends Exception
{
}
class Example5
{
public static void main(String args[])
{
int x=Integer.parseInt(args[0])
try
{
System.out.println("Hello");
if(x==1)
throw new UnnamedException();
}
catch(UnnamedException e)
{
}
finally
{
System.out.println("World");
}
}
}

It's very calm over here, why not leave a comment?

Leave a Reply