Empty catches’ blocks
-
Hi! Several days ago I debugged one service in customer machine, and so there weren’t any symbols and debug information. But this is not a big problem. The most important problem was empty catch blocks in the application. For example, try { File.Move(source, dest); } catch {} //… Other code here. It was terrible, because this service changed information in the database. And one another service tried to remove file of information is correct in the database. I’ve spent a lot time with cordbg before I’ve found issue. It was problem with user’s permissions. Empty catches are really horror. Don’t use theirs.
-
Hi! Several days ago I debugged one service in customer machine, and so there weren’t any symbols and debug information. But this is not a big problem. The most important problem was empty catch blocks in the application. For example, try { File.Move(source, dest); } catch {} //… Other code here. It was terrible, because this service changed information in the database. And one another service tried to remove file of information is correct in the database. I’ve spent a lot time with cordbg before I’ve found issue. It was problem with user’s permissions. Empty catches are really horror. Don’t use theirs.
Ooh - this is one of those issues where people come down on one side or the other. Some people seem to like silently consuming exceptions so that they don't actually have to do any *hard* coding to deal with the exceptions - they should be hunted down and tarred and feathered.
Deja View - the feeling that you've seen this post before.
-
Hi! Several days ago I debugged one service in customer machine, and so there weren’t any symbols and debug information. But this is not a big problem. The most important problem was empty catch blocks in the application. For example, try { File.Move(source, dest); } catch {} //… Other code here. It was terrible, because this service changed information in the database. And one another service tried to remove file of information is correct in the database. I’ve spent a lot time with cordbg before I’ve found issue. It was problem with user’s permissions. Empty catches are really horror. Don’t use theirs.
Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace
There are 10 types of people in the world, those who understand binary and those who dont.
-
Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace
There are 10 types of people in the world, those who understand binary and those who dont.
-
Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace
There are 10 types of people in the world, those who understand binary and those who dont.
If you have one of those, go hunting for an alternate approach that does not throw an exception. In .NET 1.x, it appeared there was no way to convert a string to an integer without getting an exception if it failed. However, you could go via the
Double
type'sTryParse
method and then cast theDouble
to anint
.Double
cannot represent the whole range of a 64-bit integer so this approach isn't suitable for numbers this large. Thankfully .NET 2.0 added a whole load ofTryParse
methods for the other basic types. (They do much the same work as the oldDouble.TryParse
).Stability. What an interesting concept. -- Chris Maunder
-
Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace
There are 10 types of people in the world, those who understand binary and those who dont.
Oooohh! The "Damn the torpedoes!" approach to coding, huh? Funny, I just about never write an exception handler that didn't actually handle the exception. If your logic depends on an exception happening, you haven't solved the logic problem properly.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Everybody is probably gonna hate me for saying this but there are times when you don't want anything to happen when you catch an exception. I do it often. I put comments in the braces to explain why though. Please don't hunt me down. ;P Peace
There are 10 types of people in the world, those who understand binary and those who dont.
-
Oooohh! The "Damn the torpedoes!" approach to coding, huh? Funny, I just about never write an exception handler that didn't actually handle the exception. If your logic depends on an exception happening, you haven't solved the logic problem properly.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007FUnny!! The same thing happens at my office all the time, and I even posted an article about it! http://sharpdeveloper.net/content/archive/2007/05/25/do-not-eat-exceptions.aspx Please do read and comment. THank you
-
FUnny!! The same thing happens at my office all the time, and I even posted an article about it! http://sharpdeveloper.net/content/archive/2007/05/25/do-not-eat-exceptions.aspx Please do read and comment. THank you
Thankfully, there's no place to vote on it. THAT'S an article??! I've seen more information on a frickin' sticky note! :laugh:
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
I hate you.
Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]
Look, most of the time it is possible to do things without an exception being thrown but sometimes it is just too much work to prevent it. Therefore catch (Exception){} I mean why bother looking for alternative approaches when try-catching is so easy. -- modified at 2:05 Tuesday 5th June, 2007
There are 10 types of people in the world, those who understand binary and those who dont.
-
Thankfully, there's no place to vote on it. THAT'S an article??! I've seen more information on a frickin' sticky note! :laugh:
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Look, most of the time it is possible to do things without an exception being thrown but sometimes it is just too much work to prevent it. Therefore catch (Exception){} I mean why bother looking for alternative approaches when try-catching is so easy. -- modified at 2:05 Tuesday 5th June, 2007
There are 10 types of people in the world, those who understand binary and those who dont.
smyers wrote:
I mean why bother looking for alternative approaches when try-catching is so easy.
Because it's so damn hard to debug. If you're gonna eat the exception, at least put some logging code in catch block.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
smyers wrote:
I mean why bother looking for alternative approaches when try-catching is so easy.
Because it's so damn hard to debug. If you're gonna eat the exception, at least put some logging code in catch block.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
dnh wrote:
Because it's so damn hard to debug.
No ways, when I have a bug that I can't find I just copy and paste MessageBox.Show(ex.ToString()); into all my empty catches(normally only two or three). Then I comment them out afterwards...sorted :)
There are 10 types of people in the world, those who understand binary and those who dont.
-
dnh wrote:
Because it's so damn hard to debug.
No ways, when I have a bug that I can't find I just copy and paste MessageBox.Show(ex.ToString()); into all my empty catches(normally only two or three). Then I comment them out afterwards...sorted :)
There are 10 types of people in the world, those who understand binary and those who dont.
Then someone start using your code, and spend whole day finiding empty catch buried under tons of code. :sigh: Been there, cursed a lot. :)
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
Then someone start using your code, and spend whole day finiding empty catch buried under tons of code. :sigh: Been there, cursed a lot. :)
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
agreed. If you actually have to eat an exception, make your catch as narrow as possible to let any other exceptions flow up to error reporting systems. You can use string matching off the messagetext to narrow things down even if the base exception is being thrown instead of something more focused.
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
dnh wrote:
Because it's so damn hard to debug.
No ways, when I have a bug that I can't find I just copy and paste MessageBox.Show(ex.ToString()); into all my empty catches(normally only two or three). Then I comment them out afterwards...sorted :)
There are 10 types of people in the world, those who understand binary and those who dont.
Then put the
MessageBox.Show(ex.ToString());
inside conditional code (does VB not have that?).catch ( System.Exception ex )
{if DEBUG
MessageBox.Show(ex.ToString());
endif
}
-
Then put the
MessageBox.Show(ex.ToString());
inside conditional code (does VB not have that?).catch ( System.Exception ex )
{if DEBUG
MessageBox.Show(ex.ToString());
endif
}
Yes, it does. The problem is that just eating exceptions willy-nilly is horrifyingly bad practice. It makes someone comming up behind you to maintain your code want to hunt you down and kill you!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Yes, it does. The problem is that just eating exceptions willy-nilly is horrifyingly bad practice. It makes someone comming up behind you to maintain your code want to hunt you down and kill you!
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Doing anything willy-nilly tends to lead to problems. All decisions require thought and review.
-
Thankfully, there's no place to vote on it. THAT'S an article??! I've seen more information on a frickin' sticky note! :laugh:
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Dave Kreskowiak wrote:
I've seen more information on a frickin' sticky note!
:laugh::laugh::laugh:
-
Hi! Several days ago I debugged one service in customer machine, and so there weren’t any symbols and debug information. But this is not a big problem. The most important problem was empty catch blocks in the application. For example, try { File.Move(source, dest); } catch {} //… Other code here. It was terrible, because this service changed information in the database. And one another service tried to remove file of information is correct in the database. I’ve spent a lot time with cordbg before I’ve found issue. It was problem with user’s permissions. Empty catches are really horror. Don’t use theirs.
Nasty. I've run into problems with these when maintaining code.
Kevin