Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
Unless you extend the Exception object... nope.

For questions about VS .net extensibility, please fire at me! :)

For readability, please use the [ CS][/CS ] tags

Posted

There was an old C++ macro, __LINE__, that would return the line number in code of the point where you called the macro. Is there anything like that in C#?

 

I know that isn't quite what microcephalic was looking for, but I think this is somewhat related.

  • *Gurus*
Posted

Unless the application is compiled in debug mode and the *.pdb files are present, you're not going to be able to retrieve the line numbers regardless of what you do. There's generally no need to do this anyway. If the method in question is so long that you can't find the problem line of code, you need to refactor, refactor and refactor some more.

 

If it is purely informational or logging purposes then you could use the exception's StackTrace property.

Exactly.

Posted

I was just curious if it could be done. I thought it might help me locate the line that contained the offending code so I could fix it. I'm working with a remote user that is experiencing an issue and could be caused by something in couple of different places in the code. I can't reproduce the error, so I was just going to send him a version of my app that would present a message box with the line number where the problem was occuring.

 

I'll just put something in a try, catch that will indicate the offending code instead. I was really just wondering if vb had something like that built in and automated already.

Posted
I was just curious if it could be done. I thought it might help me locate the line that contained the offending code so I could fix it. I'm working with a remote user that is experiencing an issue and could be caused by something in couple of different places in the code. I can't reproduce the error' date=' so I was just going to send him a version of my app that would present a message box with the line number where the problem was occuring.[/quote']

Why don't you just try using the StackTrace property that was mentioned earlier? If you display that, you'll be getting the line number and a lot more. You'd have no problem identifying where the offending code is and what called it.

Posted

What about using assertions? That will show you the line number, yes? In the exception case where you already know something has gone wrong you can just assert something that is false to force it to throw.

 

But this is all horrible practice. Why don't you just do some analysis and determine the fault? There is no reason why the client should be able to tell you anything about fixing your bug other than how to reproduce it. Unless you put in real assertions, and even then, if you're throwing assertions in prdoduction level code...it's just not the most professional thing.

 

You are catching the exception so that is a good thing. The line number though is only going to tell you where the exception was caugt. You still don't know what caused the error. I would take Derek Stone's advice and do some refactoring if you can't figure out where the exception is.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...