Monday, March 13, 2006

blaming it on something else, other than your application

this article about ASP.NET 2.0 Unhandled Exception Issues reminded me of people that always tries to blame it on something else when their application fails, not that I like how the exceptions are handled in ASP.NET 2.0, but I've heard programmers complain about the network's fault, that the third party DLL is not behaving properly, that the drivers are corrupt, even that Windows is *crashing* (imagine that! =oP)

in the case of the article it refers to unhandled exceptions killing your application, truth is, when problems arise a lot of developers will look at something else before looking into their code to see what's wrong

if you just made a change to your program and now is not working, 99.999 it was your fault!, just go look at the changes you made and fix it! instead of wasting time blaming it on something else

if an exception is killing your application and you are left with nothing but criptic messages about the error, it still is your fault, go and put proper exception handling, exception handling should be there from the time you first write the code, why leave it at the end? or why leave it until you are not able to determine the cause of the errors?
whenever you are about to write code that can potentially raise exceptions, always write this code first (adapt it to whatever language you're using)

try
catch
finally

and then write the code in between

in the catch:
handle exceptions, re-raise if needed, include any additional information that will help you track what caused this exception (line #, record #, whatever data you were dealing with, etc, it will make it a LOT easier to track if you include this information)

in the finally:
release any resources

for Delphi I like to use
try try
except
//*** handle exceptions
end finally
//***release any resources
end

C# also allows you to use a "using" statement
using (somevariable = new... blabla) {
}

which is basically an implicit
somevariable = new blabla
try
//***your code here
finally {
somevariable.Dispose();
}

so use it whenever possible

unhandled exceptions are a BIG cause for software failures, so handle your exceptions and stop blaming it on something else

No comments: