Java Reference

Java Reference Home | Smart Soft Home

throws

Description

Used to declare that a method may throw exception. This is only necessary for checked exceptions. Checked exceptions MUST be enclosed in a try/catch or declared in the method's throws clause. Unchecked exceptions MAY be enclosed in a try/catch or declared in the method's throws clause.

Example

public void setLength(int length) throws IllegalArgumentException {
  if(length < 10){
    throw new IllegalArgumentException("Length must be greater than 10");
  }
  else{
    this.length = length;
  }
}

See Also

throw
try
catch
finally