Is it possible to modify the runtime exceptions or the java checked exceptions in java. If we cannot why is it we cannot?
Regs Jacob
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Well, yes, in some ways. What sort of modification are you trying to do?
"I'm not back." - Bill Harding, Twister
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
posted
0
Originally posted by thomas jacob: Is it possible to modify the runtime exceptions or the java checked exceptions in java. If we cannot why is it we cannot?
Regs Jacob
By modify ,do you mean extending.Iy yes then you can certainly do.
Originally posted by thomas jacob: Is it possible to modify the runtime exceptions or the java checked exceptions in java. If we cannot why is it we cannot?
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.
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
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
[�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.
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
posted
0
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 ]