What is better to use for Exception output message dynamic generation String or StringBuilder or StringBuffer?
What is better to use for Exception output message dynamic generation String or StringBuilder
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
StringBufferhas been mostly replaced withStringBuilder, which is faster because it’s not synchronized.Stringis fine unless you are doing a lot of concatenation.Since you’re asking this in the context of
Exceptionmessage generation, I’d say keep it simple and useString. Well-designed systems shouldn’t throwExceptionso often that the performance benefit ofStringBuilder/StringBuffermatters.That is, the frequency of exceptions should be low enough that how the detailed message is constructed has little effect on overall performance. “Premature optimization is the root of all evil”. Only optimize sections that need it, based on profiling.
If your code spends significant amount of time dynamically generating
StringforException, then there’s something really wrong with your design. Optimizing that part of the process wouldn’t do much good because exceptions are costly to construct anyway (e.g. the stack trace capture portion).