Click here to Skip to main content
15,881,803 members
Articles / .NET
Tip/Trick

Exception Handling - Not Just a Good Idea

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
20 Dec 2011CPOL2 min read 18.3K   1   6
You have everything you need to do your job as a developer. You just need to use them.

So there I was, converting a group of legacy ASP web pages into a single ASP.Net web page. This web page presented several files for the user to download, and depending on the query string, the files' extensions change. One of the extensions is .TXT, and in this case, I had need to display slightly different text depending on whether or not the file's size was 0 (for the other two extensions that are supported, if the file is there, it's always greater than 0 length). For the .TXT files, I was using Server.MapPath to get the file's physical path on the server, and the FileInfo object to determine the file's size.


Coding was relatively uneventful, but when I started testing the TXT file variant of the page, I was getting a rather cryptic error message when the page tried to render:



The application has encountered an error.



After about 20 minutes of chasing Server.MapPath around, I narrowed the problem down to the use of the FileInfo object.

I could create a FileInfo object, but when I tried to use the Length property, I would get the error message cited above. In a last ditch effort to find out why this was happening, I put a try/catch block around the code in question, and was IMMEDIATELY notified that the file couldn't be found. This led to the 2nd realization that the page was in a different location, so NONE of the files were where I assumed they'd be, which in turn led to changes to a DTS package and a utility on another server.

My first reaction was to wonder why the object's constructor didn't throw an exception if the file couldn't be found, and I suppose the answer to that lies deep within the belly of the Redmond beast that spawned the evil that is .Net. On the other hand, it led me to share this tip:


Use all of the tools at your disposal.
Your job will honestly become that much easier.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralRe: ISanti , I'd say that naming a class SomethingInfo is kind o... Pin
Remi BOURGAREL26-Dec-11 23:49
Remi BOURGAREL26-Dec-11 23:49 
GeneralRe: No, I wasn't inspecting the file first. I think this is the... Pin
#realJSOP20-Dec-11 23:24
mve#realJSOP20-Dec-11 23:24 
GeneralBecause there is a function Exists() an a function Create() ... Pin
Remi BOURGAREL20-Dec-11 21:56
Remi BOURGAREL20-Dec-11 21:56 
GeneralRe: Yes, and the name of the class is "FileInfo": info about the... Pin
ISanti26-Dec-11 23:10
ISanti26-Dec-11 23:10 
GeneralNo languages I know of will EVER throw an exception in the c... Pin
JamesHollowell20-Dec-11 12:30
JamesHollowell20-Dec-11 12:30 
GeneralRe: Exceptions can be thrown from constructors in .Net. JSOP's p... Pin
AspDotNetDev20-Dec-11 13:28
protectorAspDotNetDev20-Dec-11 13:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.