如何在try/catch块中重新实例化变量

huangapple go评论59阅读模式
英文:

How to re instantiate variables in try/catch block

问题

我正在进行Cucumber测试并且我有以下内容
    ResponseEntity<OrderGet> createResponse;
    try {
        createResponse = api.createAddress(headers, address);
    } catch (FeignException e) {
        System.out.println(e.status());
        scenarioContext.put("createResponseValidationCode", e.status());
        scenarioContext.put("createResponseValidation", createResponse);
    }
在最后一行,Intellij显示错误,表示```createResponse```可能尚未初始化。当我用```null```进行初始化并添加```if (.. != null)```时,它永远不会执行,它仍然保持为null并且不会重新实例化该变量。```try```块中的那一行始终会抛出异常。我应该使用什么方法?
英文:

I am working on a Cucumber test and I have the following:

        ResponseEntity&lt;OrderGet&gt; createResponse;
        try {
            createResponse = api.createAddress(headers, address);
        } catch (FeignException e) {
            System.out.println(e.status());
            scenarioContext.put(&quot;createResponseValidationCode&quot;, e.status());
            scenarioContext.put(&quot;createResponseValidation&quot;, createResponse);
        }

At the last line I get error for Intellij saying that createResponse might have not been initialized. When I initialize it with null and I add an if ( .. != null) it never executes it, it still remains null and does not re instantiate the variable. The line in the try block always throws exception. What approach should I use here?

答案1

得分: 2

也许你不太理解这段代码是如何工作的。

如果catch块执行了,那意味着try块没有完全执行(或者最多只执行了一部分)。

具体来说,如果catch块被调用,那就意味着try块中的1到n个语句已经执行了,而最后一个执行的语句(注意,这不一定是try块中的最后一个语句!)没有成功完成。

鉴于在你的try块中实际上只有一个调用(调用了createAddress方法),那就是那个没有成功工作的调用。

换句话说,首先根本就没有响应对象!因此,不可能发送不存在的响应。你根本没有ResponseEntity<OrderGet>的实例。在我们达到那一步之前,代码就已经失败了。

如果你这样做:

ResponseEntity<OrderGet> createResponse = null;

…那么显然在catch块中,createResponse 总是保持为null,而在那里进行的 if (x != null) 判断将根本不会触发。

用简单的话来说:就像这样,你让一个面包师傅烤面包。

然后面包师傅告诉你:啊,糟糕,伙计。我没面团了,抱歉。然后你说:没问题。把面包给我,我会告诉我那个让我给他们拿面包的客户,这个面包无法烤出来。此时,面包师傅说:你说什么,伙计?我压根就没有面包

英文:

Perhaps you don't understand how this code works.

If the catch block executes, that means the try block did not (or did at most partially).

Specifically, if the catch block ends up being involved at all, that means 1 to n of all the statements of the try block have been executed so far, and the last statement executed (which does NOT have to be the last statement in the try block!) did not finish successfully.

Given that there's really only one invoke in your try block (which is invoking the createAddress method), that is the one that DID NOT WORK.

In other words, there is no response object in the first place! - and therefore, it is not possible to send a non-existent response. You have no instance of ResponseEntity<OrderGet>. At all. Code failed before we got that far.

If you did this instead:

ResponseEntity&lt;OrderGet&gt; createResponse = null;

... then obviously in the catch block, createResponse would always remain null, and an if (x != null) deal in there would simply mean it would never trigger.

Put in simple terms: It's like this, you ask a baker to bake some bread.

Then the baker tells you: Aw, shoot, mate. I ran out of dough, sorry. You then go: That's no problem. Just give me the bread and I'll tell the client that asked me to get them some bread that this bread could not be baked. At which point the baker goes: You what, mate? I don't have any bread!

huangapple
  • 本文由 发表于 2020年9月2日 22:51:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63708114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定