Some C# code that makes me sick...
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
try catch statements should only be used in the UI/client layer, not in business layer or data layer - usually. you should ALWAYS log your exceptions to a database or file log without exception. you should not use try/catch/exceptions to control logic flow, whenever possible. my 2 cents. the code you referenced is crap IMHO. instead of re-throwing the exception, it should be logged, and handled gracefully and informatively for the end user.
-
I always think that putting an exclamation mark at the end of an error message is the equivalent of adding "you idiot!". "Value must be numeric, you idiot!" "Date must be in d-ddd:mmm/yyyyy format, you idiot!" "Credit card number must not include spaces, you idiot!" (world's most annoying message: why can't you remove the spaces yourself?) Or in "success" messages: "File has been saved!" (like it's some sort of special achievement, and the computer is being extra kind to you in doing it)
I actually know someone who puts an exclamation behind pretty much every message box! Or three in case of an error!!! Really childish and unprofessional and this is a smart guy with good business instincts (but no programming instincts whatsoever :laugh: ).
Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
"And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk..." That's the Java Spring way, and 128 other exceptions ...
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
The code I support came with hundreds of
try
{
... do stuff ...
}
catch (Exception ex)
{
throw ex;
}I spoke to the original developer, but they are still doing the same thing even now.
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
Agree. I did like Sanders fix though. However the sub or function that they are calling is defined as a despicable act. Maybe you should have complained about that as well. :)
"Rock journalism is people who can't write interviewing people who can't talk for people who can't read." Frank Zappa 1980
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
Your code is not the worst sample of "logic"! :)
try {
DoX();
}
catch (Exception ex) {
MessageBox.Show("Something happen!");
}THIS code is a poison of modern apps!! NOBODY knows what happen, where happen, just "close app" and say goodbye to all your work. Even monkeys from MS (I remember - monkeys, SELECTED by HR!) do such things. And when you report to MS "you have Error 0x45454582354", they (like imbeciles) advice you to reboot computer. HEY!! It's your program, get off my cookies, cache, operating system and codecs - fix YOUR failures! damn...
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
If you don't trap the error, well... it will just splat on the user screen. Catching, and deciding what to do, log, or show... will help you build new code, because you will easily be able to fix your old code :) Having said all that, the code block frustrates me too. Its a bit ugly, and tedious. A while back they made properties "easier" with all that get / setter code. I would like to see an auto Trap block, or at least a simpler model for it. Keep It Simple, keep it moving.
-
If you don't trap the error, well... it will just splat on the user screen. Catching, and deciding what to do, log, or show... will help you build new code, because you will easily be able to fix your old code :) Having said all that, the code block frustrates me too. Its a bit ugly, and tedious. A while back they made properties "easier" with all that get / setter code. I would like to see an auto Trap block, or at least a simpler model for it. Keep It Simple, keep it moving.
Just so you know, the code does throw an error, the catching is just to add the useless information "I am here" and rethrow... So, your comment, as far as I understand, is irrelevant to the situation...
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
The code I support came with hundreds of
try
{
... do stuff ...
}
catch (Exception ex)
{
throw ex;
}I spoke to the original developer, but they are still doing the same thing even now.
Oooo... this is doubly terrible :( - useless try/catch - hiding the original stack! :(( :(( :(( :~
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
I have seen too. Sometimes adding context is helpful (most of the times it doesn't). If we need to add context, you can use
Exception.Data
to add context if really really needed. https://docs.microsoft.com/en-us/dotnet/api/system.exception.data?view=net-5.0Maybe I, Maybe U, can make a change to the world!
-
Just so you know, the code does throw an error, the catching is just to add the useless information "I am here" and rethrow... So, your comment, as far as I understand, is irrelevant to the situation...
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
OK I think I see your point now - someone codes a trap and really doesn't do anything with it but comment? Is that it? Keep It Simple, keep it moving.
-
I have seen too. Sometimes adding context is helpful (most of the times it doesn't). If we need to add context, you can use
Exception.Data
to add context if really really needed. https://docs.microsoft.com/en-us/dotnet/api/system.exception.data?view=net-5.0Maybe I, Maybe U, can make a change to the world!
Yes, but in that case it's not happening! ;)
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
OK I think I see your point now - someone codes a trap and really doesn't do anything with it but comment? Is that it? Keep It Simple, keep it moving.
Indeed.... It goes further, as I explained tersely... This is recursively done in multiple method that catch and rethrow each other, making the code more verbose and slower and less debugable without any benefit...
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
Thank you for your post. It got me thinking. Maybe I should place a try at each method's entry and a catch at each method's exit so as to add the method's signature and argument list to the exception message. A one line try at entry and a one line catch at exit doesn't offend my sense of aesthetics. Unfortunately as far as I know it would require knowing the types of exceptions thrown by each called method and I don't know how to do that w/o it being a pain in the rear. Perhaps this could be automated. I don't see why not. Something to cogitate on. For now though I have some hopefully exceptional code to write. - Cheerio
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
I have seen too. Sometimes adding context is helpful (most of the times it doesn't). If we need to add context, you can use
Exception.Data
to add context if really really needed. https://docs.microsoft.com/en-us/dotnet/api/system.exception.data?view=net-5.0Maybe I, Maybe U, can make a change to the world!
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
If they logged, or added something useful to the new Exception (let's say there were parameters to this method, and the exception method was more like ["Having trouble doing X for " + param.ToString()]), then maybe I could see an excuse for a catch and throw. As you show it, you're right to burn it to the ground.
-
try catch statements should only be used in the UI/client layer, not in business layer or data layer - usually. you should ALWAYS log your exceptions to a database or file log without exception. you should not use try/catch/exceptions to control logic flow, whenever possible. my 2 cents. the code you referenced is crap IMHO. instead of re-throwing the exception, it should be logged, and handled gracefully and informatively for the end user.
-
I have seen too. Sometimes adding context is helpful (most of the times it doesn't). If we need to add context, you can use
Exception.Data
to add context if really really needed. https://docs.microsoft.com/en-us/dotnet/api/system.exception.data?view=net-5.0Maybe I, Maybe U, can make a change to the world!
More times than I can count, I have seen debugging and fix time for a production issue cut from days to hours, or hours to minutes, because the appropriate runtime variables were added as name-value pairs to the exception's Data collection. That reduced time is gold to a software project during the full SDLC.
-
Thankfully I have a robust constitution and delete it viciously... But I thought I should purge here for all to see! The kind of code below makes me sick to the bone... When I see it I need to immediately incinerate this try/catch never to be caught again!
try {
DoX();
}
catch (Exception ex) {
throw new Exception("Having problem Doing X!", ex);
}And don't give me the horseradish about "but they use this opportunity to do logging and stuff". I mean this exact code above! No additional stuff! :O And speaking of logging, I have seen such try/catch/logging being nested zillion of time resulting in zillion of log entry for one single exception... yuk... :sigh: All of that can surmised with this simple hypothetical dialog Q: (Newbye Dev) How do I throw an exception in case of exception? A: (Captain Obvious) Just don't catch the goddamn exception you donkey!
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
:omg:
Pete McNamee "True knowledge exists in knowing that you know nothing."