简体   繁体   English

我们能否在不影响服务层的情况下处理从dao层到表示层的异常(服务层中没有任何修改)

[英]Can we handle a exception from dao layer to presentation layer without impacting the service layer(no modification in service layer)

The scenario is like I wrote a MVC-based application: Controller - Service - DAO layer. 该场景就像我编写了一个基于MVC的应用程序:控制器-服务-DAO层。 Now I got an exception in DAO layer and I want to handle that exception in presentation layer so that service layer needs no change. 现在,我在DAO层中有一个异常,我想在表示层中处理该异常,以便服务层无需更改。

Because by using regular try/catch or throws it has to pass through the service layer which I don't want. 因为通过使用常规的try/catchthrows它必须通过我不需要的服务层。

Is there any better approach to achieve it? 有没有更好的方法来实现呢?

class Controller{
  method1(){}
}

class service(){
  method1Serice(){}
}

class DAO(){
  method1DAO(){
  // exception occurs here
}

You can have the DAO class throw an unchecked exception. 您可以让DAO类抛出未经检查的异常。 (any subclass of RuntimeException will do). RuntimeException任何子类都可以)。 You can create your own custom exception or use any of the predefined ones. 您可以创建自己的自定义例外,也可以使用任何预定义的例外。 just make sure the Service doesn't catch Throwable and you can have the Controller catch it. 只需确保Service没有捕获Throwable并且您可以让Controller捕获它即可。

You can extend your exception class from RuntimeException so that compiler does not complain about exception handling. 您可以从RuntimeException扩展异常类,以便编译器不会抱怨异常处理。 You can then catch that exception in the presentation layer. 然后,您可以在表示层中捕获该异常。

Perhaps you are looking for Controller Based Exception Handling , you can check these in Exception Handling in Spring MVC and also Error Handling for REST with Spring . 也许您正在寻找基于Controller Based Exception Handling ,可以在Spring MVC的异常处理中检查这些内容,也可以在Spring 的REST中检查错误

@ExceptionHandler(YourException.class)
public String handleException(){
    logger.info(message);
    return "database_error";
}

Actually I would suggest you handle the exceptions properly in Service-Layer and encapsulate that exception properly to return to the front-user via Controller-Layer . 实际上,我建议您在Service-Layer正确处理异常,并正确封装该异常,以通过Controller-Layer返回给前端用户。

Normally, checked exceptions are carrying some meaningful messages which can be used to do recovery or let the caller explicitly handle it properly. 通常, checked exceptions携带一些有意义的消息,这些消息可用于进行恢复或让调用方显式正确地对其进行处理。 Try not to directly avoid it since it's there. 尽量不要直接避免它,因为它在那里。

As I understand it, a data access object (DAO) is intended for transferring data between the server and the client. 据我了解, 数据访问对象 (DAO)用于在服务器和客户端之间传输数据。 I'm assuming that the client is what you refer to as the presentation layer . 我假设客户端就是您所说的表示层 In other words, the part that the end user interacts with. 换句话说,最终用户与之交互的部分。 As such, the DAO should contain fields and accessor methods only, ie it should not contain logic. 这样,DAO应该只包含字段和访问器方法,即它不应包含逻辑。 Hence it should not contain methods that may throw exceptions. 因此,它不应包含可能引发异常的方法。 So I would suggest perhaps re-designing your application. 所以我建议也许重新设计您的应用程序。 Otherwise, perhaps you can provide more detailed code? 否则,也许您可​​以提供更详细的代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM