What is a good way to pass useful state information to an exception in Java?

前端 未结 11 1872
说谎
说谎 2021-02-04 06:23

I noticed some confusion initially with my question. I\'m not asking about how to configure a logger nor how to use a logger properly, but rather how to capture all of the infor

11条回答
  •  孤独总比滥情好
    2021-02-04 07:10

    If you want to somehow process the details of the error message, you could:

    • Use an XML text as the message, so you get a structured way:

      throw new UnhandledException(String.format(
          "Unexpected things%s", value), bthe);
      
    • Use your own (and one for every case) exception types to capture variable information into named properties:

      throw new UnhandledValueException("Unexpected value things", value, bthe);
      

    Or else you could include it in the raw message, as suggested by others.

提交回复
热议问题