Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Exception Handling - Not Just a Good Idea

, 20 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
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.

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 20 Dec 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid