What is the best way to return error message from function?
.net, c#
Solution
The better way is to throw custom exception. That's why they were introduced. If you need to provide specific info, like `ErrorCode` or something else, you could easily extend base `Exception` class to do so. Main reasons are:
- You can ignore invalid error code returned from your funcion and this could lead you to the situation where your system state is corrupted whereas `Exception` is something you can't ignore.
- If your funcion does something usable then it should return some data you interested in and not the error codes, this gives you more solid design.
Problem
If face a logic error error such `(Expired user, invalid ID)`, then what is the best way to tell the parent method of this error from the following : 1- Throwing customized exception like the following : ``` try { //if (ID doesn't match) then Throw new CustomException(-1,"ID doesn't match"); } catch(CustomException ex) { throw ex } catch(Exception ex) { throw new CustomException(ex.ErrorCode,ex.message); } ``` 2- return error message and code like : ``` //if (ID doesn't match) then This.ErrorCode= -1; This.Message= "ID doesn't match"; ```