Can I get a line of code that threw an exception?

Besides reading the GetFileName() file and reading the stack frame from the GetFileLineNumber() line from

 new StackTrace(exception).GetFrame(x) 

Is there a way to get the actual line of code (like a line) that caused the exception?

thanks

+4
source share
6 answers

Debugging moretem mails is difficult, but not impossible. There are tools that you can use ( here and here for example), as well as methods .

0
source

I do not think so. The code compiles, so it is no longer available. However, if you also have source code, there may be several workarounds.

+2
source

No, this is your only option, as far as I know. To get the source line of code that caused the exception, the source must be available. The fact that StackFrame already allows you to get the line number (through debugging characters - the PDB file in most cases) makes it simple enough, I would say.

Is there any specific problem with your proposed method?

+1
source

There is no reliable way to do this, because line information is not stored in DLL files. Information that maps IL code blocks to lines of source code is stored in a PDB file. You need to access the PDB to get row information for exception.

There is enough information about the StackFrame class to get the appropriate ISymUnmanagedMethod class if a PDB is available. Basically you need a method token and the current offset in the method. This requires you to understand the internal structure of the PDB, and I'm not sure if it is documented anywhere.

PDB API: http://msdn.microsoft.com/en-us/library/ms233503.aspx

+1
source

It will be compiled, so at runtime you will only get IL. At best, you can get IL and decompile it back to C #, just like a reflector.

+1
source

You can get the string using the try catch :

 catch(Exception exception) { //exception.StackTrace at the first line has the line you are looking for } 
0
source

All Articles