2

I have a function that requests a person, returning the person ID. However, if method detects a fault, I want to return an error (With a description?).

So, lets say my function is:

 public int GetPersonId(string username)
 {
     //Logic
     return personId;
 }

Within the logic, I call a proc, and return an ID. However, if no results were returned, I could return -1, and handle that in the calling code - but feel this isn't good.

Would it be better the create an exception, and try/catch it, or what? I'm sure I read once, that throwing exceptions for business type rules, isn't good practise.

What's the best way to deal with this.

Additionally, maybe the proc will return other statuses, such as 'person exists, but is marked as deleted', 'no such person exists' etc. What I mean is there is more than one 'exception'.

Craig
  • 18,074
  • 38
  • 147
  • 248
  • possible duplicate of [Choosing between exception and return value](http://stackoverflow.com/questions/5460101/choosing-between-exception-and-return-value) – Ken White Jul 12 '12 at 00:51
  • 1
    To return "The ID" or "Nothing" *I* would generally use `int?` and return `null` on failure -- However, if returning is more applicable than throwing an Exception depends on "what sort of effect" such a failed operation should have. In particular, the big decision question for me is: Is it *expected* to fail or does failure indicate a *coding error*? Unexpected situations and coding errors should generally result in an Exception, imoho. –  Jul 12 '12 at 00:51
  • 2
    There is nothing wrong with using -1 as an error code. – Steve Wellens Jul 12 '12 at 00:53
  • Steve - I agree, but I have more than one 'status' I need back. So, I could use 'error codes'. -1 = Not exists, -2=exists, but deleted... etc. However, the parameter is called 'personId', and it seems bad to use a parameter for holding an ID, to rather hold a error code. – Craig Jul 12 '12 at 01:15
  • 1
    @Craig I would *not* use an ID to represent a value *or* an arbitrary error code. If there are multiple "status codes" that can occur (with or without also returning an ID), indicate them through another means. –  Jul 12 '12 at 03:51

4 Answers4

1

I can think of 2 options

  1. Throw Exception inside function

    throw new Exception("Some Message");

  2. Change Return type. Instead of int return int?. If there is an error return null else value.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

Yes you can create you own Exception by inheriting the Exception class or also do it this way throw new Exception("Error in Person.GetPersonId"); As long as you catch them and log them in to an error logger it shoul dbe fine becuase you will see the message either way

If this is the only place where you want to report back with message then use the inline code i have shown above but if you will require it often and want customise exception for different types then please create your own excpetion as good practise

HatSoft
  • 11,077
  • 3
  • 28
  • 43
0

I think that you need to distinguish very clearly what is an error in your function and what is not. If the fact that no results are found is an error (only you can define if it is or not), then throw an exception. If it is not an error (because is a possible output), then return a code or whatever you want. The calling code should manage it. But don't use exception handling for dealing with the logic of a program. It is incorrect

Javier
  • 2,818
  • 3
  • 19
  • 20
0

1.If you want to return some messages, you can change your return type to string. And you will handle string type in Calling proc. 2.You can throw the exception with message, like

throw new  Exception("what's wrong")

And you can use try/catch to catch it!

Prince
  • 199
  • 1
  • 3
  • 14