This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Sets the stack trace elements that will be returned by getStackTrace() and printed by printStackTrace() and related methods. This method, which is designed for use by RPC frameworks and other advanced systems, allows the client to override the default stack trace that is either generated by fillInStackTrace() when a throwable is constructed or deserialized when a throwable is read from a serialization stream.
Parameters: stackTrace - the stack trace elements to be associated with this Throwable. The specified array is copied by this call; changes in the specified array after the method invocation returns will have no affect on this Throwable's stack trace. Throws: NullPointerException - if stackTrace is null, or if any of the elements of stackTrace are null Since: 1.4
Calling the setStackTrace method isn't something just anyone can do. Creating a StackTraceElement is not easily done, since the constructor is private. You can only get a reference to StackTraceElement objects by calling getStackTrace on a Throwable (which would give you an array of StackTraceObjects).
There is also the fillInStackTrace method, which fills in the stack trace.
Why no more setters? Well, as has been pointed out, you always have the possibility of wrapping an Exception in your own favorite MyException, with or without a message, with or without a Throwable cause, or just rethrowing it. (In case of RuntimeExceptions, rethrowing is automatic unless there is a 'catch RuntimeException'.)
An Exception is a piece of information from the system - something is wrong. This information should be acted upon - either by rethrowing, or wrapping and rethrowing, or something else instead (retrying the operation, ignoring, whatever...), but the information itself should not be modified. Modifying the existing exception would mean you are overriding what the system is telling you - which could mean loss of information. Loss of information is generally considered a "bad thing".
It would be as if you sent me an email, which I then modified and sent on to someone else. This person would see the forwarded message with its modified text, but unless I had the courtesy of marking what I modified, would assume the entire text was from you.
Hope this helps. [ December 19, 2006: Message edited by: �dne Brunborg ]
Entia non sunt multiplicanda praeter necessitatem
Joined: Jan 30, 2000
[�dne]: There is a method called setStackTrace defined in Throwable. As far as I know, this is the only setter available in either Throwable, Exception or RuntimeException.
There is also initCause() - slightly unusual in that it may only be called once, but it's still a mutator method. No idea if this is the sort of thing thomas was asking about, and after over two weeks, I'm not really expecting clarification any time soon.
Joined: Nov 29, 2005
Originally posted by Jim Yingst:
There is also initCause() - slightly unusual in that it may only be called once, but it's still a mutator method.
Probably that is the reason why its also known as Legacy constructor. [ December 19, 2006: Message edited by: Rahul Bhattacharjee ]