Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to raise an exception or return null?

I have a few functions on Data access layer

public Order RetrieveById(int id)
public List<Order> RetrieveByStatus(OrderStatus status)

Now i am bit confuse on exception raising.

In case of RetrieveById function, id which is less than 1 is an invalid id therefore i feel like raising an exception. And i feel like returning null for the Id which doesn't exist in the database. Then it feels like i am over complicating.

In case of RetrieveByStatus, i feel like returning a empty list when there is no data in the database for that status.

However i have seen that some people raising an exception when RetrieveById cannot return anything but then RetrieveByStatus should not raise exception when there is no record or should it?

Could anyone please clarify these concepts for me?

like image 892
user1767363 Avatar asked Oct 26 '12 04:10

user1767363


People also ask

Should I throw exception or return null?

Throw an ExceptionThrowing is the right approach when null represents an unrecoverable failure. For example, imagine calling a helper method that gives you a database connection string. Imagine the method doesn't find the connection string, so the method gives up and returns null .

Why is throwing an exception better than returning an error value?

When you code using return codes, you're preparing yourself for failure, and hope your fortress of tests is secure enough. When you code using exception, you know that your code can fail, and usually put counterfire catch at chosen strategic position in your code.

Is throwing an exception the same as returning a value?

It's not possible to both throw an exception and return a value from a single function call. Perhaps it does something like returning false if there's an error, but throwing an exception if the input is invalid.

When should we throw exception in Java?

The throw keyword is useful for throwing exceptions based on certain conditions e.g. if a user enters incorrect data. It is also useful for throwing custom exceptions specific to a program or application. Unchecked exceptions can be propagated in the call stack using the throw keyword in a method.


3 Answers

In the first case i would possibly go for a exception and handle myself,instead of returning null.What if your first method is used in a way that the returned object is saved to a Order reference.There is a very high chance of NullReferenceException being thrown,when someone tries to call a method or property on that object.

For the second method i would go for a empty list as some have suggested.

like image 177
Prabhu Murthy Avatar answered Oct 22 '22 03:10

Prabhu Murthy


I would prefer to return null in the first case and an empty list in the second case.

But if you want to raise exception then You can raise exception for public Order RetrieveById(int id) because it means that id is not valid as calling the first method means that you know the id and the it needs to be there.

In the second case the OrderStatus might be valid and there is not record found against it so returning an empty list is a good idea.

like image 42
Asif Mushtaq Avatar answered Oct 22 '22 01:10

Asif Mushtaq


  1. Read MSDN first: Creating and Throwing Exceptions (C# Programming Guide). It lists both situations when you are expected to throw an exception, and when to avoid it.
  2. Also take into account What is the real overhead of try/catch in C#?

In any case you'll have to process either null return or an exception thrown


As for myself, I'd prefer in both your methods not to throw exception explicitly. I'd say, there is nothing bad, if your method returns null, if it failed to find an object by id. Whereas the RetrieveByStatus method could return an empty collection, not null.

Besides you could follow the pattern used in LINQ, where you have, say, Enumerable.First and Enumerable.FirstOrDefault methods (either throwing an exception or returning null), so you could use the appropriate one in a certain situation, when the id is 100% valid or when on the contrary it could be missing. While methods returning a sequence of elements don't throw exceptions if the sequence to return appears to be empty; consider Enumerable.Where.

like image 3
horgh Avatar answered Oct 22 '22 01:10

horgh