1

I have found a really beautiful error, that can be very useful for readers.

@Override
public void storeInputStream(InputStream inputStream, FileAccessDetail fileAccessDetail) throws StorageException {
    try (FileOutputStream out = new FileOutputStream(getPathFromDescriptor(fileAccessDetail))) {
        IOUtils.copy(inputStream, out);
    } catch (FileNotFoundException e) {
        throw new StorageException("Unable to save inputrstream to location " + fileAccessDetail.toString(), e);
    } catch (IOException e) {
        throw new StorageException(e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {}
    }
}

A stream is created and remembered for closing later, but overwritten by copy of another stream. So, both copies of that another stream will be closed, but the first stream - never.

Can I publish it on SO as a question and my answer?

5
  • 6
    I guess you can, if you can actually formulate a question, because right now I don't see one, just a code snippet and a description of what actually happens.
    – BoltClock Mod
    Commented Nov 28, 2017 at 15:24
  • 2
    I'm not sure what the "beautiful" error is? Why is there a stream passed to the function when it is not used anywhere?
    – BDL
    Commented Nov 28, 2017 at 15:37
  • @BDL It is a piece of the very real project that is a grandchild of an extremely large project, used by many companies from the Forbes list. Simply by accident I have found an old error.
    – Gangnus
    Commented Nov 28, 2017 at 15:55
  • 1
    Your question sounds to me like a overriding problem of streams. But if you ask the question and use this code sample for demonstration, then the obvious first answer will be "There is a huge design error if you pass a variable to a function that isn't used inside". All other problems just arise because you do that. I'm not sure how this is an interesting problem. Or better to say: I wouldn't know how to formulate the question in a way that it can be found by people running into something similar.
    – BDL
    Commented Nov 28, 2017 at 16:22
  • Thank you, everybody. Of course, I knew about the possibility to answer my own questions as such. I was not so sure about that question could be accepted. The answer was rather negative, but anyway useful. So, I have to find how to formulate this question/answer to look more interesting. Of course, this is NOT a duplicate, for asks about this concrete example of question. But it is senseless to struggle against this sort of people...
    – Gangnus
    Commented Nov 30, 2017 at 8:06

1 Answer 1

9

If the question has not already been asked, then sure. If it has been asked already, then yours is likely to be closed as a duplicate.

Just to be clear, asking a self-answered question should be done only when you can ask it in such a way that others may possibly answer; asking a question only you could possibly answer is typically not OK.

And BoltClock makes a good, albeit obvious, point with his comment - make sure you ask an actual question.

6
  • Not only does it need to be possible for other people to answer it, but it needs to be a quality question by all of the site's standards, just as it would be if it weren't answered by the author, meaning it needs to be well researched, clear, appropriately scoped, on topic, not subjective, all of it.
    – Servy
    Commented Nov 28, 2017 at 15:29
  • @Servy Indeed; hopefully OP would abide by that regardless, as it applies to any question, not just self-answers.
    – TylerH
    Commented Nov 28, 2017 at 15:32
  • 1
    So many people seem to think that "self answers are special" that I've long since stopped assuming people will think that. In my experience people knowing that without being told is the exception, not the rule.
    – Servy
    Commented Nov 28, 2017 at 15:33
  • "asking a self-answered question should be done only when you can ask it in such a way that others may possibly answer" - a good point. I see such "closed for others" question as impoliteness, but it should be in rules, IMHO. Is it there?
    – Gangnus
    Commented Nov 30, 2017 at 8:11
  • @Gangnus I'm not sure; I'd have to look. It's more likely that it's in a FAQ-style Meta post than in the Help Center, though.
    – TylerH
    Commented Nov 30, 2017 at 14:23
  • @TylerH I feel ashamed that you'll spend your time on that. I thought, you can already know. Sorry.
    – Gangnus
    Commented Nov 30, 2017 at 14:54

Not the answer you're looking for? Browse other questions tagged .