• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Exceptions

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes, in some ways. What sort of modification are you trying to do?
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can write your own exception by extending the exception class
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By modifying, do you mean that you would catch a NPE then rethrow it as another exception?
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic