Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is returning null after exception is caught bad design [closed]

I always come across the same problem that when an exception is caught in a function that has a non-void return value I don't know what to return. The following code snippet illustrates my problem.

public Object getObject(){   try{     ...     return object;   }   catch(Exception e){     //I have to return something here but what??     return null; // is this a bad design??   } } 

So my questions are:

  • Is return null bad design?
  • If so what is seen as a cleaner solution??

thanks.

like image 843
woolagaroo Avatar asked Jan 30 '10 15:01

woolagaroo


People also ask

Is returning null bad design?

In Clean Code by Robert Martin he writes that returning null is bad design when you can instead return, say, empty array. Since expected result is an array, why not? It'll enable you to iterate over result without any extra conditions. If it's an integer, maybe 0 will suffice, if it's a hash, empty hash.

Is it bad practice to return null Java?

Returning Null is Bad Practice The FirstOrDefault method silently returns null if no order is found in the database. There are a couple of problems here: Callers of GetOrder method must implement null reference checking to avoid getting a NullReferenceException when accessing Order class members.

Is it better to return null or throw exception?

Only throw an exception if it is truly an error. If it is expected behavior for the object to not exist, return the null. Otherwise it is a matter of preference. It certainly shouldn't be a matter of preference.

Can we return null in catch block?

Is returning null in catch block is best practice.. Best is a relative word. If you want to swallow / disregard the exception then you would return null. i.e. you cannot do anything about the exception but still want to continue processing then this might be "best".


2 Answers

I would say don't catch the exception if you really can't handle it. And logging isn't considered handling an error. Better to bubble it up to someone who can by throwing the exception.

If you must return a value, and null is the only sensible thing, there's nothing wrong with that. Just document it and make it clear to users what ought to be done. Have a unit test that shows the exception being thrown so developers coming after you can see what the accepted idiom needs to be. It'll also test to make sure that your code throws the exception when it should.

like image 120
duffymo Avatar answered Sep 18 '22 18:09

duffymo


I always come across the same problem that when an exception is caught in a function that has a non-void return value I don't know what to return.

If you don't know what to return, then it means that you don't know how to handle the exception. In that case, re-throw it. Please, don't swallow it silently. And please, don't return null, you don't want to force the caller of the code to write:

Foo foo = bar.getFoo(); if (foo != null) {     // do something with foo }  

This is IMHO a bad design, I personally hate having to write null-checks (many times, null is used where an exception should be thrown instead).

So, as I said, add a throws clause to the method and either totally remote the try/catch block or keep the try/catch if it makes sense (for example if you need to deal with several exceptions) and rethrow the exception as is or wrap it in a custom exception.

Related questions

  • How to avoid “!= null” statements in Java?
like image 29
Pascal Thivent Avatar answered Sep 16 '22 18:09

Pascal Thivent