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
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.