-1

How do I provoke an error so that the exc.printStackTrace(System.out); is displayed when I run the program:

public String getName(){
    try {
        if(haveName()) return Name;
        else {
            throw new CorrectNameException("no name");
        }
    } catch (ExistException e) {
        return e.getExc();
    } catch (CorrectNameException exc) {
        exc.printStackTrace(System.out);
        return "nameless";
    }
}
4

2 回答 2

1

Make haveName() return false. Simple.

于 2019-03-24T13:29:05.943 回答
0

According to what you posted in question, answer would be "When haveName() method returns false"

But If you explain complete use case, we can rectify your code a little bit. As per my understanding your method is checking two things.

  • Name exist or not?
  • Name is valid or not?

So I have couple of question regarding your query:

  • Does method haveName() check for existence and validity both? (or only for existence).
  • Are you throwing any exception inside haveName() method?
于 2019-03-24T14:09:10.643 回答