Java 程式設計/關鍵字/throw
出現
throw 是一個關鍵字;它 '丟擲' 一個異常。在 throw 語句中,可以丟擲的三種類型的物件是:Exception、java:Throwable 和 java:Error
語法
throw <Exception Ref>;
例如
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
}
|