Note: although seemingly similar, this is not a duplicate of http://stackoverflow.com/questions/1520859/overriding-fillinstacktrace-for-a-standard-jvm-exceptions
What are your thoughts on overriding fillInStackTrace on checked exceptions to return null?
The advantages are:
- you can use said exception for control flow without the performance penalty of filling in a stack trace.
- You can pass an arbitrary object back to the caller as a property of the exception, regardless of the signature of the method that throws said exception. (e.g. an error message, a partly-initialed value, etc.)
The biggest disadvantage is simply that this is not standard practice.
But the only way of doing something similar is:
- returning null, or a "special value" back (e.g. -1) to signify something went wrong.
- changing the callee to return a composite object of the value & error code, and having the caller inspect this object to see what happened.
I'm looking for an arguments that are extremely convincing for or against the practice of overriding fillInStackTrace, or an argument that shows it's no big deal either way.
Apparently RIFE uses this technique:
http://www.rifers.org/
http://cretesoft.com/archive/newsletter.do?issue=129
Below is a standard way of doing something, and a contrived example using this technique.
//standard
int i = "foo".indexOf('f');
if (i<0){
System.out.println("found f");
} else {
System.out.println("no f here");
}
//example
try {
"foo".indexOf('f');
System.out.println("found f");
} catch (NotFound e) {
System.out.println("no f here");
}