Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

catching method and line number in exception

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"
strStatus+=ex.Message;
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
Andre
0
andremara
Asked:
andremara
  • 2
2 Solutions
 
Dale BurrellDirectorCommented:
Well the Exception class does have additional information (http://msdn.microsoft.com/en-us/library/system.exception_members(v=VS.90).aspx) including the stack, source, sometimes an inner exception, and sometimes a more specific exception e.g. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception_methods(v=VS.90).aspx

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.
0
 
andremaraAuthor Commented:
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.
0
 
databoksCommented:
Print the StackTrace.
Try

{
  //Code..
}
catch(Exception ex)
{
  MessageBox.Show(ex.StackTrace);

}

Open in new window


Just using MessageBox.Show(); as illustration.
0
 
andremaraAuthor Commented:
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!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now