通过抛出异常来控制Java流量(Java flow control by throwing exceptions)

[db:摘要]

通过抛出异常来控制Java流量(Java flow control by throwing exceptions)

关于优选的流量控制方法,我有一个问题困扰了我一段时间。

我经常遇到一种情况,我必须根据null或非not null方法的返回值来决定做什么

所以我有两个选项,我知道如何处理它:

检查为空:

public class BasedOnNullFlowControl { public String process(String login) { String redirectUri = getRedirectUri(login); if (redirectUri != null) { return redirectUri; } else { return "Your login is taken"; } } private String getRedirectUri(String login) { Optional<Login> loginFromDb = checkLoginExists(login); if (loginFromDb.isPresent()) { return null; } else { return "some-redirect-url"; } } private Optional<Login> checkLoginExists(String login) { return Optional.empty(); //assume this value comes from some API } private class Login {} }

或者例外中断流程:

public class ExceptionFlowControl { public String process(String login) { return getRedirectUri(login); } private String getRedirectUri(String login) { Optional<Login> loginFromDb = checkLoginExists(login); loginFromDb.ifPresent(l -> { throw new LoginExistsException(); }); return "some-redirect-url"; } private Optional<Login> checkLoginExists(String login) { return Optional.empty(); } private class LoginExistsException extends RuntimeException { } private class Login {} }

我知道,异常应该仅在特殊情况下使用,但我的情况并不例外,第二种解决方案看起来对我来说更好,因为我可以添加一些异常处理程序(如在Spring中)并将其转换为一些不错的Http状态代码结束。 控制器方法process没有被几十个空检查污染。

请问聪明人请告知应该使用哪种解决方案? 或者可能还有第三个?

I have a question that is bothering me for a while about the preferable flow control method.

I often encounter a situation, where I have to decide what to do based on return value from a method which is null or not null

So I have two options which I know about, how to deal with it:

Check for null:

public class BasedOnNullFlowControl { public String process(String login) { String redirectUri = getRedirectUri(login); if (redirectUri != null) { return redirectUri; } else { return "Your login is taken"; } } private String getRedirectUri(String login) { Optional<Login> loginFromDb = checkLoginExists(login); if (loginFromDb.isPresent()) { return null; } else { return "some-redirect-url"; } } private Optional<Login> checkLoginExists(String login) { return Optional.empty(); //assume this value comes from some API } private class Login {} }

Or interrupt the flow with exception:

public class ExceptionFlowControl { public String process(String login) { return getRedirectUri(login); } private String getRedirectUri(String login) { Optional<Login> loginFromDb = checkLoginExists(login); loginFromDb.ifPresent(l -> { throw new LoginExistsException(); }); return "some-redirect-url"; } private Optional<Login> checkLoginExists(String login) { return Optional.empty(); } private class LoginExistsException extends RuntimeException { } private class Login {} }

I know, that exceptions should be used only in exceptional situations, but my situation is not exceptional and still second solution looks better for me, because I can add some exception handler (like in Spring) and translate it to some nice Http status code in the end. The controller method process is not contaminated with dozens null checks.

Could you smart people please advise which one solution should be used? Or maybe there is a third one?

发布者:admin,转转请注明出处:http://www.yc00.com/web/1690545856a367649.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信