为什么RouteInfoManager.java使用嵌套try。。。接住

正如主题所说。我不知道为什么代码使用嵌套try。。。catch,可能是因为解锁操作会引发异常? 示例代码如下:

现在的代码:

public byte[] getAllTopicList() {
    TopicList topicList = new TopicList();
    try {
        try {
            this.lock.readLock().lockInterruptibly();
            topicList.getTopicList().addAll(this.topicQueueTable.keySet());
        } finally {
            this.lock.readLock().unlock();
        }
    } catch (Exception e) {
        log.error("getAllTopicList Exception", e);
    }

    return topicList.encode();
}

期待如下:

public byte[] getAllTopicList() {
    TopicList topicList = new TopicList();
    try {
        this.lock.readLock().lockInterruptibly();
        topicList.getTopicList().addAll(this.topicQueueTable.keySet());
    } catch (Exception e) {
        log.error("getAllTopicList Exception", e);
    } finally {
        this.lock.readLock().unlock();
    }
    return topicList.encode();
}

原提问者GitHub用户shenshaoming c

展开
收起
芬奇福贵 2023-05-26 11:03:52 94 分享 版权
1 条回答
写回答
取消 提交回答
  • 我同意你的观点,解锁确实可以抛出异常。

    原回答者GitHub用户lizhiboo

    2023-05-26 17:23:32
    赞同 展开评论
问答标签:
问答地址: