no throws in method signature when methods generates exception?

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

no throws in method signature when methods generates exception?

Post 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...

admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 188 guests