Java Reference

Java Reference Home | Smart Soft Home

throw

Description

throw is similar to return in that both are keywords used to exit a method. return can return any data type (or nothing at all). Throw can only return an object of type Throwable. Any code after the throw will not be executed.

Example

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