Page 1 of 1

no throws in method signature when methods generates exception?

Posted: Wed Nov 13, 2013 4:19 pm
by javaman
class Elliptical{
public int radiusA, radiusB;
public int sum = 100;

public void setRadius(int r){
if(r>99) throw new IllegalArgumentException();
radiusA = r;
radiusB = sum - radiusA;

}
}
Why doesn't the compiler complain that
public void setRadius() doesn't read
public void setRadius() throws IllegalArgumentException??? The method throws this error...

Re: no throws in method signature when methods generates exception?

Posted: Wed Nov 13, 2013 4:59 pm
by admin
IllegalArgumentException is a RuntimeException, which does not need to be caught or declared in the throws clause.

You may want to read up more on checked and unchecked exception: http://javarevisited.blogspot.com/2011/ ... -java.html and
http://stackoverflow.com/questions/6115 ... xplanation

HTH,
Paul.