I know in other languages like C/C++ you would return a value like SUCCESS which would indicate to the calling method that the method returned successfully, or failed.
Is there a “nice” way to do this in C# without needing a SUCCESS return variable? Throwing an exception comes to mind, but I am not sure how one would implement this. Is there a custom exception you could throw with your own error message?
Sample code would be greatly appreciated.
Thank you in advance.
You can derive your own exceptions from Exception.
You can return a bool or some numeric convention.
In C#, you have output parameters which are a little easier to use than pointers if you want to return success/fail, but also product output.
I tend to go with exceptions for exceptional things. Logical returns for logical things. i.e. Don’t use exceptions to control program operations. If a file operation unexpectedly fails, then throw an exception. If the app needs to look for a file and generally expects to not find it sometimes as a normal operation, I would not expect the caller to catch an exception, but instead to simply return a bool and choose an appropriate logical path through the applciation.