Click here to Skip to main content
13,351,896 members (88,698 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 20 Dec 2011

Exception Handling - Not Just a Good Idea

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.


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


About the Author

John Simmons / outlaw programmer
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.

You may also be interested in...

Comments and Discussions

GeneralRe: ISanti , I'd say that naming a class SomethingInfo is kind o... Pin
Remi BOURGAREL27-Dec-11 0:49
memberRemi BOURGAREL27-Dec-11 0:49 
GeneralRe: No, I wasn't inspecting the file first. I think this is the... Pin
John Simmons / outlaw programmer21-Dec-11 0:24
mvpJohn Simmons / outlaw programmer21-Dec-11 0:24 
GeneralBecause there is a function Exists() an a function Create() ... Pin
Remi BOURGAREL20-Dec-11 22:56
memberRemi BOURGAREL20-Dec-11 22:56 
GeneralRe: Yes, and the name of the class is "FileInfo": info about the... Pin
ISanti27-Dec-11 0:10
memberISanti27-Dec-11 0:10 
GeneralNo languages I know of will EVER throw an exception in the c... Pin
JamesHollowell20-Dec-11 13:30
memberJamesHollowell20-Dec-11 13:30 
GeneralRe: Exceptions can be thrown from constructors in .Net. JSOP's p... Pin
AspDotNetDev20-Dec-11 14:28
protectorAspDotNetDev20-Dec-11 14: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 20 Dec 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid