开发者

Catching Exceptions From Multiple Methods Withing the Class

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜