Multiple Catch blocks that do the same thing...
-
Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...
try
{
DoSomething();
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)
-
They don't occur exactly with the same reason. Off course both catches exception but how each one will occur determine by the actual method logic. Suppose Dosomething() has DB process, then where you will think possible to be catched if error happens. On catch (Exception ex) block and where do you think the socket error will be catched on (SocketException sex). That is why the method logic determine the exception occurence.
It doesn't matter. Regardless of what exception is thrown, the code executed will be identical. Which means that the separate catch for the "lesser" exception need not be there at all. And if it needed be there, why put it in? All that does is leave a "hole" where duplicated code can become different over time and cause other problems - that's one of the advantages of inheritance: it removes the need to "copy and paste" code by reusing a single method in all derived classes. So if the method needs to be changed, it's in one single place instead of scattered all over the file and prone to being missed when the updates are done.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
They don't occur exactly with the same reason. Off course both catches exception but how each one will occur determine by the actual method logic. Suppose Dosomething() has DB process, then where you will think possible to be catched if error happens. On catch (Exception ex) block and where do you think the socket error will be catched on (SocketException sex). That is why the method logic determine the exception occurence.
I assume we're talking past each other. The question isn't if catch blocks catching different types of exceptions can be useful but if it's of any use in this concrete case - which it isn't because the exceptions aren't handled differently.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
It doesn't matter. Regardless of what exception is thrown, the code executed will be identical. Which means that the separate catch for the "lesser" exception need not be there at all. And if it needed be there, why put it in? All that does is leave a "hole" where duplicated code can become different over time and cause other problems - that's one of the advantages of inheritance: it removes the need to "copy and paste" code by reusing a single method in all derived classes. So if the method needs to be changed, it's in one single place instead of scattered all over the file and prone to being missed when the updates are done.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
We all know that managed .Net exception are derived from Exception class. If you are asking why the rest of derived Exception such as socketexception, Dbexception are created then that is another question. And it won't be necessary to allow multiple catch statement block from the compiler. Off course if you leave/comment out the above socketexception then the error definitely go to exception block, b/c of the reason that I stated above. Onething sure to know here, you can't determine to refactor the code by looking that code only, even we don't know either a SocketException will occur or not. G. Day
-
There is a recommended practice where you catch the minimum exception you can: SocketException instead of Exception for example, but that code's silly. It's possible that he planned to come back and do something different for the SocketException later and never got round to it, but if that's the case the author still needs a thump upside the head... :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
OriginalGriff wrote:
It's possible that he planned to come back and do something different for the SocketException later and never got round to it
That was my thought exactly. :thumbsup:
There are only 10 types of people in the world, those who understand binary and those who don't.
-
Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...
try
{
DoSomething();
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)
-
We all know that managed .Net exception are derived from Exception class. If you are asking why the rest of derived Exception such as socketexception, Dbexception are created then that is another question. And it won't be necessary to allow multiple catch statement block from the compiler. Off course if you leave/comment out the above socketexception then the error definitely go to exception block, b/c of the reason that I stated above. Onething sure to know here, you can't determine to refactor the code by looking that code only, even we don't know either a SocketException will occur or not. G. Day
It doesn't matter if a SocketException or an AliensLandedOnWhiteHouseLawnException occurs with that code: the code that is executed is identical regardless. That is the point. Not that Socket Exceptions are derived from Exception - we all know that - but that having a separate catch block is silly if the code executed is the same anyway! Look at the code. Assume a SocketException occurs in the method. What lines of code are executed? Now assume that a AliensLandedOnWhiteHouseLawnException occurs instead. What lines of code are executed? :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
It doesn't matter if a SocketException or an AliensLandedOnWhiteHouseLawnException occurs with that code: the code that is executed is identical regardless. That is the point. Not that Socket Exceptions are derived from Exception - we all know that - but that having a separate catch block is silly if the code executed is the same anyway! Look at the code. Assume a SocketException occurs in the method. What lines of code are executed? Now assume that a AliensLandedOnWhiteHouseLawnException occurs instead. What lines of code are executed? :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
It does matter, a self ref [^] How do you know the code executed the same ? YOU DON'T KNOW WHAT THE METHOD(Dosomething()) DO. What we know both exception was not utilized as such.
-
It does matter, a self ref [^] How do you know the code executed the same ? YOU DON'T KNOW WHAT THE METHOD(Dosomething()) DO. What we know both exception was not utilized as such.
Member 11394652 wrote:
YOU DON'T KNOW WHAT THE METHOD(Dosomething()) DO
Seriously, it doesn't matter. Look at the code we do have:
catch (SocketException e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}The code in each catch block is identical. :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...
try
{
DoSomething();
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)
Just useful while debugging; you can have a breakpoint in one block but not the other; but it should be removed afterward.
-
Member 11394652 wrote:
YOU DON'T KNOW WHAT THE METHOD(Dosomething()) DO
Seriously, it doesn't matter. Look at the code we do have:
catch (SocketException e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}The code in each catch block is identical. :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Are you saying the message will always be the same ? This is a trick, If you do get it. :)
-
They don't occur exactly with the same reason. Off course both catches exception but how each one will occur determine by the actual method logic. Suppose Dosomething() has DB process, then where you will think possible to be catched if error happens. On catch (Exception ex) block and where do you think the socket error will be catched on (SocketException sex). That is why the method logic determine the exception occurence.
Member 11394652 wrote:
SocketException sex
:)
Cheers, विक्रम "We have already been through this, I am not going to repeat myself." - fat_boy, in a global warming thread :doh:
-
Are you saying the message will always be the same ? This is a trick, If you do get it. :)
Yes. The message comes from the Exception object, and will the same regardless of which catch block catches it: for a SocketException it will print a socket based message, for an AliensLandedOnWhiteHouseLawnException, it will print a message in Klingon. But it is irrelevant which catch block prints it, because they both use the same code to do that. There is only any point in having separate catches if they do different things:
catch (SocketException e)
{
Console.WriteLine("Problem with socket: {0}", e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine("An unknown error occured: {0}", e.Message);
conn.connecting = false;
}Or even:
catch (SocketException e)
{
Console.WriteLine("It's life Jim: {0}", e.Message);
conn.connecting = false;
}
catch (AliensLandedOnWhiteHouseLawnException e)
{
Console.WriteLine("Klingons on the starboard bow, starboard bow, starboard bow\nKlingons on the starboard bow, starboard bow Jim!: {0}", e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine("But not as we know it: {0}", e.Message);
conn.connecting = false;
}See what we mean?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Yes. The message comes from the Exception object, and will the same regardless of which catch block catches it: for a SocketException it will print a socket based message, for an AliensLandedOnWhiteHouseLawnException, it will print a message in Klingon. But it is irrelevant which catch block prints it, because they both use the same code to do that. There is only any point in having separate catches if they do different things:
catch (SocketException e)
{
Console.WriteLine("Problem with socket: {0}", e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine("An unknown error occured: {0}", e.Message);
conn.connecting = false;
}Or even:
catch (SocketException e)
{
Console.WriteLine("It's life Jim: {0}", e.Message);
conn.connecting = false;
}
catch (AliensLandedOnWhiteHouseLawnException e)
{
Console.WriteLine("Klingons on the starboard bow, starboard bow, starboard bow\nKlingons on the starboard bow, starboard bow Jim!: {0}", e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine("But not as we know it: {0}", e.Message);
conn.connecting = false;
}See what we mean?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
The fact that you still ignore the
Dosomething()
method do, you generalize that both catches block get the same exception. Even I gave you example that the method might throw DBException and you expect the SocketException get exception based on your assumption. -
Member 11394652 wrote:
SocketException sex
:)
Cheers, विक्रम "We have already been through this, I am not going to repeat myself." - fat_boy, in a global warming thread :doh:
:)
-
The fact that you still ignore the
Dosomething()
method do, you generalize that both catches block get the same exception. Even I gave you example that the method might throw DBException and you expect the SocketException get exception based on your assumption.I don't need to know what DoSomething() does to reason this: original code: SocketException > catch block 1 > { Console.WriteLine(e.Message); conn.connecting = false; } other Exception > catch block 2 > { Console.WriteLine(e.Message); conn.connecting = false; } after removing catch block 1: SocketException > catch block > { Console.WriteLine(e.Message); conn.connecting = false; } other Exception > catch block > { Console.WriteLine(e.Message); conn.connecting = false; } ergo: catch block 1 is redundant
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
The fact that you still ignore the
Dosomething()
method do, you generalize that both catches block get the same exception. Even I gave you example that the method might throw DBException and you expect the SocketException get exception based on your assumption.No, I don't. But both catch blocks have the same code. So they both do the same thing regardless of which exception gets fired.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...
try
{
DoSomething();
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)
Looks like code I would have written except that I would have put a TODO in the SocketException handler with a note to do something more useful than what the generic exception handler does. Or the other way around. Either way, the intention would be to come back later and fix it. Marc
Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!
-
Hi CP community. We hired a guy that did this a while ago. It annoyed me then, and now I have a project that I'm refactoring that lo and behold has it as well. Maybe I'm missing some recommended practice (I googled it), and I don't mean to start a war or anything. I just don't see an obvious use for things like this. The message is the same when a socket error occurs...
try
{
DoSomething();
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
conn.connecting = false;
}What purpose in life, universe, code, etc... does a practice like this serve?! (Clarification for all of those who've been giving concrete reasons for catching different exception types. I get that. I do that as well. I'm saying that the guy we hired previously would handle several exception types only to do the same thing as the catch-all block. Literally the same thing. Sometimes he would catch a type only to throw it to the main block doing nothing with the specific type. It bugged the hell out of me. Now I'm refactoring another project that is completely unrelated and I see a similar practice which made my mind wander to here...)
I'd lay odds that the original programmer thought he may 'legitimately' get a socketException error, so catches it and handles it appropriately. (though in this case, just logs it) But then wants to catch and log any other exception, just in case. So the fault is in the fact that in the 1st catch he should probably have been putting some relevant code (I dunno - pop up a message to the user or something) as it is an 'expected' exception. As I think Marc said - looks like a TODO is missing!
PooperPig - Coming Soon
-
I don't need to know what DoSomething() does to reason this: original code: SocketException > catch block 1 > { Console.WriteLine(e.Message); conn.connecting = false; } other Exception > catch block 2 > { Console.WriteLine(e.Message); conn.connecting = false; } after removing catch block 1: SocketException > catch block > { Console.WriteLine(e.Message); conn.connecting = false; } other Exception > catch block > { Console.WriteLine(e.Message); conn.connecting = false; } ergo: catch block 1 is redundant
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
Yes you do need to have the catch block. In fact you may need to add more based on what
Dosomething()
does. What op need to do is to implement the appropriate logic in the catch block which I already said it in my first response.