문제

In Java, I have a class with a few methods that throw the same custom exception (the custom exception extends the 'Exception' class):

private void setAColor(float r, float g, float b, float a) throws AnException {}
private void setBColor(float r, float g, float b, float a) throws AnException {}
private void setCColor(float r, float g, float b, float a) throws AnException {}

Instead of having the user handle these exceptions, I would like to be able to catch them and handle them within the class. Each method exception will be caught in the exact same way. I do not want to have to put the exact same try/catch method block in every single method. Is there a way I can catch these exceptions outside of the body of the methods, but inside of the class?

Thank you for your time, even if you cannot help me.

APPENDING MORE INFORMATION:
These methods may be called up until a certain point: After a method is called that sets the variable "isImmutable" to true, these methods should no longer be able to change anything about the object. Hence, if one of these methods are called after "isImmutable" is set to true, then they should throw an exception called "ImmutableException". For /this particular implementation/, I want to be able to handle these myself instead of going to every single point in the code that these methods are called and handling them then exact same way.

도움이 되었습니까?

해결책

I do not want to have to put the exact same try/catch method block in every single method.

Then don't throw the exception.

Is there a way I can catch these exceptions outside of the body of the methods, but inside of the class?

No.

For /this particular implementation/, I want to be able to handle these myself instead of going to every single point in the code that these methods are called and handling them then exact same way.

That makes no sense. If an error is detected, how do you intend to report it?

After a method is called that sets the variable "isImmutable" to true, these methods should no longer be able to change anything about the object. Hence, if one of these methods are called after "isImmutable" is set to true, then they should throw an exception called "ImmutableException".

Have you considered making your exception a subclass of RuntimeException? Then callers do not need to declare exception handlers -- if no handler the program will be terminated due to "unhandled exception".

다른 팁

You could turn the above methods into stub methods which call

setColorDelagator(char abc, ...)

which delegates to

setColorAWithErrorHandling
setColorBWithErrorHandling
setColorCWithErrorHandling 

within a try/catch.

I agree this isn't optimal, but I'm unsure of a better way.

To approach it from another angle, have you considered switching to a factory pattern? Rather than have something call to set isImmutable to true, have it call to get an instance where isImmutable is true... and the method in question returns a different version of the class that doesn't have the setters available.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top