File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
thomas jacob
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes, in some ways. What sort of modification are you trying to do?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sanjit Kumar
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can write your own exception by extending the exception class
 
Chengwei Lee
Ranch Hand
Posts: 884
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By modifying, do you mean that you would catch a NPE then rethrow it as another exception?
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[�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
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic