microcephalic Posted December 23, 2004 Posted December 23, 2004 If I have code that is throwing an exception, is there any way to get the line number of the offending code without having to manually specify that line number? Quote
Himo Posted December 23, 2004 Posted December 23, 2004 Unless you extend the Exception object... nope. Quote For questions about VS .net extensibility, please fire at me! :) For readability, please use the [ CS][/CS ] tags
Administrators PlausiblyDamp Posted December 23, 2004 Administrators Posted December 23, 2004 What do you need the line number for? If it is purely informational or logging purposes then you could use the exception's StackTrace property. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mskeel Posted December 23, 2004 Posted December 23, 2004 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. Quote
*Gurus* Derek Stone Posted December 23, 2004 *Gurus* Posted December 23, 2004 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. Quote Posting Guidelines
microcephalic Posted December 23, 2004 Author Posted December 23, 2004 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. Quote
Tygur Posted December 24, 2004 Posted December 24, 2004 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. Quote
mskeel Posted December 24, 2004 Posted December 24, 2004 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. Quote
*Gurus* Derek Stone Posted December 24, 2004 *Gurus* Posted December 24, 2004 If you post the code in question I'm sure one of us can offer advice on how to make it a lot more robust. Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.