Three-tier architecture: Must Exception dependencies between layers be avoided?
Following this question: Handling exceptions in a Swing UI (low level to high level and exception wrapping)
There is an obvious dependency between the logic/service and ui layer in that an Exception is being throw开发者_JAVA百科n from a method in a service class (IOException coming from a file operation) and being handled in the code of Swing component.
Is this something to be avoided? If it's best to avoid dependencies like this how could Exceptions in a service class be handled? Should I simply wrap the Exception in a RuntimeException and let it propagate to the UncaughtExceptionHandler?
If an exception is part of the public API of the service tier, then it is perfectly fine to depend on them. However, it's probably good practice to not let those exception escape passed your UI controllers and into the UI. From there you should notify the UI of what you would like to happen because of the exception.
If you save/load data it's normal to share IOException
between layers. But such exceptions like SaxParserException
/SqlException
should be wrapped because they depend from current implementation.
精彩评论