return responseEntity breaks flow

时间:2016-07-11 21:39:44

标签: java exception try-catch httpresponse stax

I am using a StAX parser, that uses the XMLStreamReader interface. XMLSteamReader.next throws the XMLStreamException.

When I return and break the flow, using a ResponseEntity instance

Ex:

catch (IOException | XMLStreamException e) {
   message = e.getMessage();
   log.debug(message);
   return new ResponseEntity<String>(message, HttpStatus.BAD_REQUEST);
}

The message I get:

[DEBUG] test.rng (No such file or directory)

When I don't return,

I get:

[DEBUG] test.rng (No such file or directory)
[DEBUG] "fservice/.../1.2.3/1/test.rng.xml" (Line 16): The end-tag for element type "elem" must end with a '>' delimiter.

Now, I need to display the second message too, as it is more descriptive. How can I do that?

1 个答案:

答案 0 :(得分:2)

The main difference between the two cases is that you return in the first, and fall through in the second.

Notice that the errors are actually different: the first is saying that a file can't be found:

[DEBUG] test.rng (No such file or directory)

The second is saying that an XML file is malformed (and notably an end tag, meaning that the start tag was found, so the file was found):

"fservice/.../1.2.3/1/test.rng.xml" (Line 16): The end-tag for element type "elem" must end with a '>' delimiter.

So the second case is actually a completely different problem, which you don't see in the first case because of the return breaking the control flow.