When to raise and when to return in case of errors

I have a function in a model manager that should return 10 random queries. What should happen if my database has only 8 records? Raise an error? Return only 8? Send some kind of error message with the results?

What is the best practice on this?

There is no “best practice” for this.

This is an application requirements issue. You need to determine why you’re doing this and what the implications are of each choice.

Hi Ken,

thanks for your response. This is for one of my Hobbie projects. I can either catch the error or use a tupple with a result or just go with the 8.

Is it totally up for grabs or is there sone pythonic way to do it?

Without seeing your model manager trying to do this, I can’t make any specific suggestions.

However, it really does come down to what you’re trying to achieve.

Does it make sense to only process 8 values? Or does there need to be 10 entries for your results to have any meaning? I can’t answer those questions - that’s something you need to determine within the context of your application. Python will let you make either choice.

1 Like