LastError
-
Long ago, LastError was a cute concept for dealing with errors, e.g. on the DOS / Windows command line. It is similar to HResult of C / C++.
Actually, we decided to do object oriented programming with C#. C# has the concept of exceptions. I.e. you can throw exceptions, and catch them somewhere else on the stack. Sometimes, some underlying code my do that for you, e.g. a FileNotFoundException.
Kevin is very creative and intelligent. He does not like the exceptions of the framework and prefers good old LastError. His latest project is full of his creatively created new pattern:
protected FileStream CreateFileStream(string _filePathName) { { try { return new FileStream(_filePathName, FileMode.Create); } catch (Exception exception) { LastError = exception.ToString(); return null; } } } private void CheckError() { if (!string.IsNullOrEmpty(LastError)) { Logger.LogImportant(nameof(SomeClass), LastError); LastError = null; } }
Hm, why not log the exception with Logger.LogException? Why log at the "important" level instead of "error" level? Why not a try...catch at some level further up in the code instead of many try...catch...return null?
Why why why...
Oh, just another line of Kevin code: Thread.Sleep(DEFAULT_TIMEOUT_SECS); (in C#, Thread.Sleep takes either a TimeSpan or an int - and in case if an int, that's the number of milliseconds...).
-
Why litter the code???
https://docs.microsoft.com/en-us/security-risk-detection/concepts/first-chance-exception
-
Ugh, the insidious "set error on a property and return a delayed null reference exception instead" anti-pattern.
I'm glad I got rid of that in our codebase.
-
@BernieTheBernie said in LastError:
good old LastError
God forbid that you have a system that has to handle multiple errors at once…
-
God forbid that you have a system that has to handle multiple errors at once…
LastError = "Something went wrong";
Easy.
-
@Applied-Mediocrity said in LastError:
God forbid that you have a system that has to handle multiple errors at once…
LastError += "Something went wrong";
Easy.
FTFY
-
@BernieTheBernie said in LastError:
good old LastError
God forbid that you have a system that has to handle multiple errors at once…
Long live threads.
Filed under:
#define errno (*(errno_location()))
-
Filed under:
#define errno (*(errno_location()))
static inline int *errno_location(void) { static int the_errno; return &the_errno; }