Let your code output the file name, row number, and column number of the exception.

Source: Internet
Author: User
Tags stack trace

CodeAs follows:

 
Try{// Your code here}Catch(ExceptionEx ){Console. Writeline (ex. Message); system. diagnostics.StacktraceTrace =NewSystem. diagnostics.Stacktrace(EX,True);Console. Writeline (trace. getframe (trace. framecount-1). getmethod (). Name );Console. Writeline (trace. getframe (trace. framecount-1). getfilelinenumber ());Console. Writeline (trace. getframe (trace. framecount-1). getfilecolumnnumber ());}

 

A little simpler, you can use exception. tostring ().

Example output of ex. tostring:

System. invalidcastexception: Unable to cast object of Type 'Microsoft. Sharepoint. splist' to type 'Microsoft. Sharepoint. spdocumentlibrary '.
At getfolderlength. program. Main (string [] ARGs) in E: \ code \ CSHARP \ project1 \ Program. CS: line 110

 

The result of system. appdomain. currentdomain. basedirectory. tostring () is as follows:

E: \ code \ CSHARP \ project1 \ bin \ debug \

 

References:

Getting line numbers in exception stack trace in a Windows Service

Http://dnchannel.blogspot.com/2007/09/getting-line-numbers-in-exception-stack.html

System. Diagnostics. stacktrace or system. Diagnostics. stackframe for debugging? Why

Http://stackoverflow.com/questions/925737/system-diagnostics-stacktrace-or-system-diagnostics-stackframe-for-debugging-w

Caveats about system. Diagnostics. stacktrace

Http://blogs.msdn.com/jmstall/archive/2005/03/20/399287.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.