Consider these 2 pieces of code (you can assume execeptionObj is of type Object, but we know it’s an instance of Throwable):
1)
logger.log(Level.ERROR, (Throwable) exceptionObj,
((Throwable) exceptionObj).getMessage());
2)
Throwable t = new Throwable((Throwable)exceptionObj);
logger.log(Level.ERROR, t, t.getMessage());
During a code review for a project I’m working on, one reviewer is saying that the first way is not as efficient as the second way because it involves 2 casts. I just wondered what you thought. It seems like creating a new instance would involve some overhead as well.
The two pieces of code do different things. In the second, you are no longer passing exceptionObj, but “an unspecified Throwable caused by exceptionObj”. I don’t think that’s what you want.
Did you mean the first line of the second one to be:
It is very marginally more efficient, I’d guess, but would not let this decide the issue. Which is more readable is the key, and I think the (modified) second one is more readable.
Do you really have to case to Throwable? surely the logger takes a Throwable already?