Replace fillInStackTrace for control / performance flow

Note: although it doesn't seem to be a duplicate Override fillInStackTrace for standard JVM exceptions

What are your thoughts on overriding fillInStackTrace on checked exceptions to return null?

Benefits:

  • you can use the specified exception for the control flow without sacrificing performance when populating the stack trace.
  • You can pass an arbitrary object back to the caller as an exception property, regardless of the signature of the method that throws the specified exception. (e.g. error message, partially initialized value, etc.).

The biggest flaw is simply that this is not standard practice.

But the only way to do something like this is:

  • returning null or a "special value" back (for example, -1) means that something went wrong.
  • changing the called party to return the composite value object and error code, and the calling object checks this object to find out what happened.

I am looking for arguments that are extremely convincing for or against the practice of overriding fillInStackTrace or an argument that doesn't really matter much anyway.

RIFE seems to use this method:

http://www.rifers.org/
http://cretesoft.com/archive/newsletter.do?issue=129

Below is a standard way to do 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"); } 
+4
source share
2 answers

If you think you need to do this, you can probably use good refactoring.

My general thought would be that there is a state of failure, that you are trying to get the tree back. If this state was set in a shared object, then the state just needs to be requested.

Are you sure that each object does only one thing? Are your objects valid in any state that they can reach?

As a goal, if your objects have more than 10 methods, with most methods making up 1 or 2 lines, and several methods filling the screen or so, you probably need more classes, and, as I said, if you are on in fact there were enough classes, and they had a consistent state, this problem would almost certainly disappear.

Example from comments: In a comment, Asser used indexOf as an example - the way he returns two values ​​to int. This will always result in some pretty straightforward code:

 int pos=str.indexOf('a'); if(pos < 0) // Not obvious from code--should comment... System.out.println("Fail"); else //Do something with pos... 

He suggests solving this with an exception that will lead to the following code:

 try { int pos=str.indexOf('a'); //Do something with pos... } catch(CharacterNotFoundInStringException e) { System.out.println("Fail"); } 

Who has the advantage that I am documenting myself more because of the created new type of exception, but it has an unusual syntax and requires the creation of a new class (exception).

My suggestion might be to create a new class as well, but program it like this:

 Position pos=str.indexOf('a'); if(!pos.characterFound()) System.out.println("Fail"); else // do something with pos.location(); 

Self-documenting, faster (stacks with exceptions are always slower), etc.

But, as I said, the biggest advantage is the new "pos" class. This will probably be very useful. In fact, instead of using pos.location, you can move the code from the else clause inside the method to pos, completely removing it.

Since it can contain business logic, this method in position can actually be executed by the .indexOf call itself, completely eliminating the if statement around the call.

This does not make much sense for "general" library methods such as indexOf. It’s unfortunate that the SDK methods are really difficult to implement in the right OO, but in your own business logic you will almost certainly find this new class β€œPosition” very useful in the future.

+3
source

I think that everything is in order. Sometimes you have no choice. But I find the code "if-else on return value" is generally easier to write and read than the code "try-catch on exception".

+1
source

All Articles