Java Programming/Keywords/throw
Appearance
From Wikibooks, open books for an open world
throw
is a keyword; it 'throws' an exception. In a throw statement, the three types of objects that can be thrown are: Exception
, java:Throwable
, and java:Error
Syntax:
throw
<Exception Ref>;
For example:
Computer code
publicCustomerfindCustomer(Stringname)throws'''CustomerNotFoundException''' { CustomercustRet=null; Iteratoriter=_customerList.iterator(); while(iter.hasNext()) { Customercust=(Customer)iter.next(); if(cust.getName().equals(name)) { // --- Customer find -- custRet=cust; break; } } if(custRet==null) { // --- Customer not found --- thrownew'''CustomerNotFoundException'''("Customer "+name+" was not found"); } returncustRet }