The throw keyword is typically used within a try block to indicate that an exceptional condition has occurred. The throw keyword is followed by an exception object, derived from the class Throwable, indicating the type of exception being thrown. A throw statement causes a program to immediately stop and resume at the nearest catch statement that can handle the specified exception object. Hence, throw statements may also be outside a try block, if the exception will be caught elsewhere within the program.
The following example shows use of the throw keyword:
public void someMethod( int div ) throws Exception1
{
try
{
if ( div == 0 )
throw new Exception1( );
}
catch(Exception1 e1)
{
// Exception can be handled in this catch
// block
.
.
.
}
}