Throw keyword

Throw keyword is used to throw exception manually.

Syntax:

	throw <exception object>;
	


Example:


class exp 
{
public static void main(String arg[])
{
int b=0;
try{
int a=4/b;
}
catch(Exception e)
{
System.out.println("Exception caught");
}
}
}

In above code exception is generated automatically on execution of a=4/b; statemnt. Above program can be written using throw keyword.

Exmaple :


class exp 
{
public static void main(String arg[])
{
try
{
throw new ArithmeticException();
}
catch(ArithmeticException a)
{
System.out.println("Exception caught");
}

}
}