Click here to Skip to main content
15,886,519 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Some C# code that makes me sick... Pin
Tecfield31-Mar-21 3:02
Tecfield31-Mar-21 3:02 
GeneralRe: Some C# code that makes me sick... Pin
agolddog31-Mar-21 3:15
agolddog31-Mar-21 3:15 
GeneralRe: Some C# code that makes me sick... Pin
PeteMcNamee31-Mar-21 7:43
PeteMcNamee31-Mar-21 7:43 
GeneralRe: Some C# code that makes me sick... Pin
englebart31-Mar-21 9:48
professionalenglebart31-Mar-21 9:48 
GeneralRe: Some C# code that makes me sick... Pin
Super Lloyd31-Mar-21 13:33
Super Lloyd31-Mar-21 13:33 
GeneralRe: Some C# code that makes me sick... Pin
JP Reyes31-Mar-21 11:30
JP Reyes31-Mar-21 11:30 
GeneralRe: Some C# code that makes me sick... Pin
Super Lloyd31-Mar-21 13:34
Super Lloyd31-Mar-21 13:34 
GeneralRe: Some C# code that makes me sick... Pin
qmartens1-Apr-21 7:24
qmartens1-Apr-21 7:24 
IMHO, the correct answer to "How should I handle exceptions?" is "Do Not." ("Unless you know how to recover from a specific exception." (e.g. FileNotFoundExceptions are easy to deal with. However, if you try to handle things like DivideByZeroException, OutOfMemoryException, or StackOverflowException, you're pretty much attached by an inclined plane wrapped helically around an axis.)

Also (I might catch (pun intended) a lot of flak for this):
  • re-throwing exceptions should be avoided like the plague, as doing so can potentially cause a single exception to be logged multiple times. Unfortunately, of the async/await (i.e. things in the System.Threading.Tasks namespace) re-throw a lot, which leads to a lot of noise and distractions while debugging).
  • a catch { ... } block is the wrong place to log exceptions.
A better alternative is use [Exception Filters] for logging. For example:
C#
try {
    Foo();
}
catch(SpecificException ex) when (ex.Log()) {
}
(Where .Log is an extension method that always returns false).

An even better alternative is to avoid logging exceptions in catch blocks or exception filters altogether. I am glad that it was decided not to remove the System.AppDomain class in .NET Core and .NET 5+ for this very reason: you can use the [AppDomain.FirstChanceException] and [AppDomain.UnhandledException] events as the one (erm... OK, two) place(s) to log all exceptions. You can even get fancy and add something to the Exception.Data dictionary in those events to avoid logging the same exception multiple times. However, in order for this to be useful, exceptions should not be re-thrown in order to ensure the logging of complete stack traces.
Eagles my fly, but weasels don't get sucked into jet engines.

GeneralRe: Some C# code that makes me sick... Pin
Lupestro4-Apr-21 5:13
Lupestro4-Apr-21 5:13 
GeneralRe: Some C# code that makes me sick... Pin
soulesurfer6-Apr-21 2:53
soulesurfer6-Apr-21 2:53 
GeneralRe: Some C# code that makes me sick... Pin
Kiriander6-Apr-21 20:31
Kiriander6-Apr-21 20:31 
GeneralRe: Some C# code that makes me sick... Pin
Member 1194113119-Apr-21 10:55
Member 1194113119-Apr-21 10:55 
JokePalindromes Pin
Cp-Coder29-Mar-21 14:00
Cp-Coder29-Mar-21 14:00 
GeneralRe: Palindromes Pin
Jacquers29-Mar-21 19:26
Jacquers29-Mar-21 19:26 
GeneralRe: Palindromes Pin
Gary Wheeler31-Mar-21 1:48
Gary Wheeler31-Mar-21 1:48 
GeneralRe: Palindromes Pin
englebart31-Mar-21 10:00
professionalenglebart31-Mar-21 10:00 
GeneralRe: Palindromes Pin
Jacquers31-Mar-21 19:33
Jacquers31-Mar-21 19:33 
GeneralRe: Palindromes Pin
Sander Rossel29-Mar-21 19:48
professionalSander Rossel29-Mar-21 19:48 
GeneralRe: Palindromes Pin
Richard MacCutchan29-Mar-21 21:22
mveRichard MacCutchan29-Mar-21 21:22 
GeneralRe: Palindromes Pin
Tony Hill30-Mar-21 3:41
mveTony Hill30-Mar-21 3:41 
GeneralRe: Palindromes Pin
Daniel Pfeffer30-Mar-21 4:31
professionalDaniel Pfeffer30-Mar-21 4:31 
GeneralRe: Palindromes Pin
Jacquers30-Mar-21 8:15
Jacquers30-Mar-21 8:15 
GeneralThe Lunacy Increases Pin
Gerry Schmitz29-Mar-21 12:10
mveGerry Schmitz29-Mar-21 12:10 
GeneralRe: The Lunacy Increases Pin
Greg Utas29-Mar-21 12:48
professionalGreg Utas29-Mar-21 12:48 
GeneralRe: The Lunacy Increases Pin
Jalapeno Bob29-Mar-21 13:39
professionalJalapeno Bob29-Mar-21 13:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.