File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

thomas jacob
Ranch Hand

Joined: May 19, 2005
Posts: 91
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: 18671
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: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
you can write your own exception by extending the exception class
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
By modifying, do you mean that you would catch a NPE then rethrow it as another exception?


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
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.

setStackTrace

public void setStackTrace(StackTraceElement[] stackTrace)

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: 18671
[�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: 2308
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 ]
 
 
subject: Exceptions