So just got into the basics of try-catch statements in Java and I\'m still a bit confused on some differences within the syntax.
Here is the code I\'m trying to anal
You can declare a method to throw an exception if you can't (or it's not convinient) to handle the exception inside the method.
In your case, you are calling the method setRadius inside the constructor. If you think that is convinient to handle the exception (that is thrown by setRadius) inside the constructor, you can use a try-catch clause:
public CircleWithException(double newRadius) throws InvalidRadiusException {
try {
setRadius(newRadius);
numberOfObjects++;
} catch (InvalidRadiusException e) {
setRadius(0); // for example
}
}
The catch block contains what you want to do if an exception were thrown. In this case, I'm setting the radius to 0, but you can change this.
Remember that it depends in your classes implementation and how you want them to work. If you don't want the constructor to handle this exception, you can throw it (as you are already doing) and handle it in other methods.