What kind of error handling is this?
Posted on October 17th, 2008
You’d be surprised the things that can be found during basic code reviews :) Once I found a pen I thought I’d lost … :) in all seriousness though, nothing drives me more crazy than seeing the following.
try { // some code } catch (Exception e) { throw e; }
I could scream! and actually it wouldn’t be the first time either. I don’t have a lot of patience for things like this.
If you aren’t sure what is wrong, let us take a closer look. For one, I came across this while actually hunting for a memory leak but I could never see an exception on any line except “throw e;” – because no matter what went wrong, the exception was caught and just thrown again and since the last line responsible is always the throw, I had to attach a debugger to find the real error.
The second problem is you haven’t actually accomplished anything, nada, zip, less than $2c of quality code here people – it doesn’t do anything. If you’re going to catch an error then DEAL with it.
I have seen some developers catch an error, log various details and then throw some kind of error again. Maybe there is a need for that but I doubt it can be justified very often. If something goes wrong you need to try fix the problem and continue normal operations so you users are unaware and if you can’t then fail gracefully.
Make sure too that you LOG errors – use any means necessary but just make sure the stack trace and some information about system state is recorded so you can fix fix fix :) – since its probably your fault it broke to start with.
And if you don’t have any error handling, get too it! Nothing could be worse than your users getting the yellow? screen of death :)
Here is something if you’re just beginning – I’ve always found these books pretty good for getting started, including some basics like the above.
![]()
Beginning Microsoft Visual C# 2008 (Wrox Beginning Guides)
Tags: .NET, Bad Code
Filed under .NET | No Comments »
Don’t do this: Exception Handling
Posted on November 23rd, 2007
I work with a lot of .NET developers and hey even some Java ones from time to time ;) We’ve all done some dumb things and we all write bugs but the following is a recurring issue I find and I wish I knew where people were learning this.
try
{
// do something bad
}
catch (Exception err)
{
throw(err);
}
If you aren’t going to do anything with an exception then why are you catching it? Make your code user friendly and actually try recover from whatever went wrong, if it’s completely unknown then log it and let the user know (if required) that something bad happened.
As for logging techniques when things do go wrong please don’t just dump it out to a log file. I’m lazy and I don’t need to go trawling around the drive looking and then searching through log files. Here is my recommended sequence to follow when trying to log application errors:
- For critical failures send an email but as with all logging, make sure you don’t end up in loops!
- Log next into either the server event logs (application log) or an internal log that the site/application admin can use. Of course this won’t work if you’re experiencing a database problem in which case go back to the event log.
- As the last resort log to the disk.
Since I write a ton of ecommerce style sites I have one fail-safe for all financial related errors – all the available data gets emailed to me so I can rebuild the required data for the user.
-c
Tags: .NET, Bad Code
Filed under .NET | No Comments »
My Vista nightmare loop
Posted on November 9th, 2007
Mostly a rant …. one of those days ;-)
So we all know that networking in Vista is screwed up and I am amazed that 1 year after release it still doesn’t work. What happens to me? well after about 8 hours of use most applications loose their internet connection (wireless and lan) – Most? you ask … yes – things like outlook still get/send mail, IE/FF stop working, chat stops etc. maybe office just “wishes” the data over the air ![]()
So I need to reboot to get back my connection – great, here is the problem. After getting the “logging off” and then “shutting down” for an un-determined amount of time my machine just blue screens … awesome.
That means for one I can never “shutdown” as the blue screen causes a restart. Now lets compound this – if I resume from Sleep or Hibernate I get an ACS (acs.exe??) crash and guess what – no internet. So I need to do the above BSOD sequence – (left eye twitching).
This is Vista certified hardware from Lenovo – “certified” I guess means Vista will run … what it does while running isn’t “certified” but at least I know my drivers are pretty good.
So once or twice a day I must force a BSOD to keep surfing, I can’t resume,sleep,shutdown or restart with a BSOD so installing updates is fun.
Boot-> Trash Network-> Reboot-> BSOD-> Recover-> Shutdown-> BSOD-> Recover-> Work-> Trash Network-> Reboot-> BSOD and so on …. how much asprine can you take in a day? ;-)
Tags: Bad Code, Vista
Filed under General, Software | No Comments »