I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 176, comments - 1796, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm a MCP and MCTS for SQL Server. I also speak at local user group meetings and conferences like NT Conference 
Welcome to my blog.

Search this Blog

My Blog Feed via Email


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

.Net: Passing user data with Exception back to the caller method

We're all familiar (i hope :)) with this construct:

try
{
    // ... some code here ...
}
catch (Exception ex)
{
    // one of these 2 lines are usually seen
    throw; // presereves the full call stack
    //throw ex; // changes the origin of exception to this method
}
finally
{ 
    // more stuff here
}

 

It's a standard error catching routine in .Net.

But what if you want to pass some user info back to the caller method with the Exception being thrown?

Reader... meet Exception.Data. Exception.Data... meet reader.

Now that you're both properly aquainted let see what it does.

Exception.Data is a dictionatry that holds object for key and value. This means that you can put anything into it.

Here's how my standard catch code part looks like:

catch (Exception ex)
{
    // pass the ex to preserve full stac trace
    Exception exNew = new Exception("New exception message", ex);
    exNew.Data.Add("string Data", "data");
    exNew.Data.Add("int data", 1);
    exNew.Data.Add("bool data", true);
    exNew.Data.Add("DateTime data", DateTime.Now);
    // full ex info with user data
    throw exNew;
}

 

I've seen a lot of code and i've never seen this being used. I have seen other way overcomplicated methods being used for the same functionality though. :)

Even though .Data is the first property in the Exception intellisense list it gets overlooked.  How? I have no idea.

 

kick it on DotNetKicks.com

Print | posted on Monday, September 24, 2007 5:36 PM

Feedback

# re: .Net: Passing user data with Exception back to the caller method

Careful if you want to use this in a Webservice!

I was told, that you can run into problems if using this in a Webservice scenario, because the serializeability is affected and the exception can't be transfered correctly.

For more information look at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1649014&SiteID=1 for example.

Besides that it is a great hint!
10/8/2007 10:57 AM | Deniz

# re: .Net: Passing user data with Exception back to the caller method

thanx for letting us know about this deniz. thanx!
10/8/2007 10:58 AM | Mladen

# re: .Net: Passing user data with Exception back to the caller method

This short article is more useful than everything I've readed about exceptions so far.
6/4/2009 11:04 PM | Manuel

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 5 and type the answer here:

Powered by: