Question: All. I am reading a book called and one of the idea is "Don't return null" when writing a method. He suggests either "throw out exception" or "use special case" when the function has to return null. 

If the method return type is a list, I know I can return a empty list instead of null. However, what if the return type is a specific object. For example, a method to search database by unique id and return the result. What should I return if the method can not find anything?

I tried to use "throw out exception" but end up writing more coded and additional logic for any place calls the function. 

Any suggestion will be appreciated.

Answer: 
Null is defined as non-existence, which seems to fit in perfectly with what you want. If your code returns nothing return nothing, right?

BUT, as always, it depends.

If your code is not meant to return nothing then doing so can cause untold problems. In this case it's definitely better to throw an exception. `1 / null`, won't work everywhere.

If non-existence is a perfectly valid return value then why would you want to return an exception? Assuming of course that you've got the code in place to deal with a non-existent value being returned from your query then there's no need to throw an exception at all.
I don't see any problem with returning null, but throwing an exception seems like the most sensible alternative. Your best bet would be to create your own, custom Exception class. 

The code should look just about the same either way:

```
try {
    SearchResult someResult = searchForStuff();
}
catch ( ResultNotFoundException rnfe ) {
    /* do stuff */
}

/* almost the same as this */

SearchResult someResult = searchForStuff();

if ( someResult == null ) {
    /* do stuff */
}

```