catching method and line number in exception

Posted on 2011-04-21
Last Modified: 2012-05-11
Hi all,
I have a webservice written in C#.NET on VS2008, with framework 3.5. I'm catching errors in my webservice with try/catch block. The catch does something like this:

catch(Exception ex)
strStatus = "Problem in xyz program"
return strStatus;

What I'd like is to return the exact method and line number that caused the problem. Is that possible? I don't want to chase things like "object not set to instance of an object" anymore!

Thank you
Question by:andremara
    LVL 21

    Accepted Solution

    Well the Exception class does have additional information ( including the stack, source, sometimes an inner exception, and sometimes a more specific exception e.g.

    If that doesn't narrow it down, add more try/catch blocks and then set your status to strStatus = "Problem in xyz program at catch block X".

    Also it sounds like you aren't coding well if you are getting lots of that sort of error. If there is a chance an object could be null your code should know that and be able to handle it.

    Author Comment

    Thanks. The ex.StackTrace method is very helpful and I'll use that.

    By the way, I only got one occurrence of the 'object not set...' error, but I was thrown since my code worked great here but errored at the client. Once I did Build CLEAN, I also got the error here and was able to track it down. Turns out that because I upgraded this code from VS2003, something didn't update behind the scenes in VS2008 and somehow it was using older code for one of my .cs files.

    All's good now. Thanks for such fast reply.
    LVL 8

    Assisted Solution

    Print the StackTrace.
    catch(Exception ex)

    Open in new window

    Just using MessageBox.Show(); as illustration.

    Author Closing Comment

    dale_burrell answered in detail and it worked. After I used his info to solve it, Databoks then gave a great answer that was very concise and targeted, although a bit less detail, which is why more points for dale. thanks to both!

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now