当前位置: 首页 > 工具软件 > dont-break > 使用案例 >

do while break 用法

公羊俊
2023-12-01

do while break

do while(false) + break 可以模拟 goto语句,遇到break直接跳转。

@RequestMapping ("/add")
    @ResponseBody
    @Override
    public Map<String, Serializable> addComment(Comment comment, String token) {
        Map<String, Serializable> result = new HashMap<>();
        result.put("status", "failure");
        try {
            do {
                Token tokenCheck = TokenUtil.checkToken(token, TokenUtil.TokenUssage.DEFAULT);
                User user = userService.findUserById(tokenCheck.getUserId());
                if (user == null) {
                    throw new TokenUtil.TokenNotFound("用户不存在");
                }
                if (comment.getVideoId() == null) {
                    result.put("msg", "视频 id 为空");
                    break;
                } else {
                    Video video = videoService.findVideoById(comment.getVideoId());
                    if (video == null) {
                        result.put("msg", "视频 id 不正确");
                        break;
                    }
                }
                if (comment.getContent() == null) {
                    result.put("msg", "评论内容为空");
                    break;
                } else if (comment.getContent().length() == 0 || comment.getContent().length() > 250) {
                    result.put("msg", "评论内容为空或超过250长度限制");
                    break;
                }
                comment.setUserId(user.getId());
                comment.setId(null);
                comment.setSendtime(ZonedDateTime.now());
                comment.setCountLike(0);
                try {
                    commentService.addComment(comment);
                    result.put("status", "success");
                } catch (Exception e) {
                    result.put("msg", "未知错误");
                }
            } while (false);
        } catch (TokenUtil.TokenExpired | TokenUtil.TokenNotFound | TokenUtil.TokenOverAuthed | TokenUtil.TokenUssageNotMatched tokenError) {
            result.put("msg", tokenError.getMessage());
        }
        return result;
    }
 类似资料: