MST

星途 面试题库

面试题:Java异常处理设计模式在分层架构中的应用

假设你正在开发一个基于三层架构(表现层、业务逻辑层、数据访问层)的Java应用程序,阐述如何在各层中运用异常处理设计模式,以保证系统的健壮性和可维护性,举例说明可能用到的具体设计模式及实现方式。
14.8万 热度难度
编程语言Java

知识考点

AI 面试

面试题答案

一键面试

表现层异常处理

  1. 设计模式:通常采用外观模式(Facade Pattern)来处理异常。外观模式提供了一个统一的接口,隐藏系统的复杂性,在表现层可以将业务逻辑层和数据访问层的异常统一处理。
  2. 实现方式:在表现层的方法中调用业务逻辑层方法时,使用try - catch块捕获异常。例如:
import com.example.businesslogic.BusinessLogic;
import com.example.businesslogic.BusinessLogicException;

public class PresentationLayer {
    public void doSomething() {
        try {
            BusinessLogic businessLogic = new BusinessLogic();
            businessLogic.executeBusinessLogic();
        } catch (BusinessLogicException e) {
            // 记录异常日志,向用户展示友好错误提示
            System.err.println("业务逻辑执行出现问题: " + e.getMessage());
        }
    }
}

业务逻辑层异常处理

  1. 设计模式:责任链模式(Chain of Responsibility Pattern)可用于异常处理。不同的业务规则处理模块可以形成一个责任链,当出现异常时,沿着责任链传递异常,每个模块可以选择处理或者继续传递。
  2. 实现方式:定义一个抽象的责任处理类,具体的业务规则处理类继承该抽象类,并实现处理异常的方法。例如:
// 抽象责任处理类
abstract class BusinessRuleHandler {
    protected BusinessRuleHandler successor;
    public void setSuccessor(BusinessRuleHandler successor) {
        this.successor = successor;
    }
    public abstract void handleBusinessLogicException(BusinessLogicException e);
}

// 具体责任处理类
class SpecificBusinessRuleHandler extends BusinessRuleHandler {
    @Override
    public void handleBusinessLogicException(BusinessLogicException e) {
        if (e.getErrorCode() == 1) {
            // 处理特定错误码1的异常
            System.err.println("特定业务规则处理模块处理错误码1的异常: " + e.getMessage());
        } else if (successor != null) {
            successor.handleBusinessLogicException(e);
        }
    }
}

// 业务逻辑类
public class BusinessLogic {
    public void executeBusinessLogic() throws BusinessLogicException {
        // 模拟业务逻辑执行中抛出异常
        throw new BusinessLogicException(1, "业务逻辑异常");
    }
}

// 业务逻辑异常类
class BusinessLogicException extends Exception {
    private int errorCode;
    public BusinessLogicException(int errorCode, String message) {
        super(message);
        this.errorCode = errorCode;
    }
    public int getErrorCode() {
        return errorCode;
    }
}

在业务逻辑执行时,构建责任链并处理异常:

public class Main {
    public static void main(String[] args) {
        SpecificBusinessRuleHandler handler1 = new SpecificBusinessRuleHandler();
        SpecificBusinessRuleHandler handler2 = new SpecificBusinessRuleHandler();
        handler1.setSuccessor(handler2);
        try {
            BusinessLogic businessLogic = new BusinessLogic();
            businessLogic.executeBusinessLogic();
        } catch (BusinessLogicException e) {
            handler1.handleBusinessLogicException(e);
        }
    }
}

数据访问层异常处理

  1. 设计模式:采用策略模式(Strategy Pattern)。针对不同类型的数据库操作异常(如连接异常、SQL语法异常等),定义不同的处理策略。
  2. 实现方式:定义一个异常处理策略接口,不同的具体策略类实现该接口。例如:
// 异常处理策略接口
interface DataAccessExceptionStrategy {
    void handleException(DataAccessException e);
}

// 连接异常处理策略类
class ConnectionExceptionStrategy implements DataAccessExceptionStrategy {
    @Override
    public void handleException(DataAccessException e) {
        if (e instanceof ConnectionException) {
            // 处理连接异常,如尝试重新连接
            System.err.println("处理连接异常: " + e.getMessage());
        }
    }
}

// SQL语法异常处理策略类
class SqlSyntaxExceptionStrategy implements DataAccessExceptionStrategy {
    @Override
    public void handleException(DataAccessException e) {
        if (e instanceof SqlSyntaxException) {
            // 处理SQL语法异常,如提示语法错误信息
            System.err.println("处理SQL语法异常: " + e.getMessage());
        }
    }
}

// 数据访问异常类
class DataAccessException extends Exception {
    public DataAccessException(String message) {
        super(message);
    }
}

// 连接异常类
class ConnectionException extends DataAccessException {
    public ConnectionException(String message) {
        super(message);
    }
}

// SQL语法异常类
class SqlSyntaxException extends DataAccessException {
    public SqlSyntaxException(String message) {
        super(message);
    }
}

// 数据访问类
public class DataAccessLayer {
    private DataAccessExceptionStrategy strategy;
    public DataAccessLayer(DataAccessExceptionStrategy strategy) {
        this.strategy = strategy;
    }
    public void executeDataAccess() throws DataAccessException {
        // 模拟数据访问操作抛出异常
        throw new ConnectionException("连接数据库失败");
    }
    public void handleException(DataAccessException e) {
        strategy.handleException(e);
    }
}

在数据访问层使用时:

public class Main2 {
    public static void main(String[] args) {
        DataAccessExceptionStrategy strategy = new ConnectionExceptionStrategy();
        DataAccessLayer dataAccessLayer = new DataAccessLayer(strategy);
        try {
            dataAccessLayer.executeDataAccess();
        } catch (DataAccessException e) {
            dataAccessLayer.handleException(e);
        }
    }
}