Java Programming/Keywords/throw

From Wikibooks, open books for an open world
< Java Programming‎ | Keywords
Jump to: navigation, search

throw is a keyword. It 'throws' an exception.

Syntax:

throw <Exception Ref>;

For example:

Computer code
public Customer findCustomer( String name ) throws '''CustomerNotFoundException'''
 {
    Customer custRet = null;
 
    Iterator iter = _customerList.iterator();
    while ( iter.hasNext() )
    {
        Customer cust = (Customer) iter.next();
        if ( cust.getName().equals( name ) )
        {
           // --- Customer find --
           custRet = cust;
           break;
        }
     }
     if ( custRet == null )
     {
        // --- Customer not found ---
        throw new '''CustomerNotFoundException'''( "Customer "+ name + "was not found" );
     }
 
    return custRet
  }

See also: