Answer the question
In order to leave comments, you need to log in
Is it possible to get the line number where the C# exception occurred?
Hello. How to get the line number where the exception occurred in a try-catch construct?
You can get the method name like this
System.Reflection.MethodBase.GetCurrentMethod().Name
I would also like to get the line number :)
Answer the question
In order to leave comments, you need to log in
The [CallerLineNumber] attribute. To get the method name, by the way, there is a similar attribute - [CallerMemberName], and for a file - [CallerFilePath]
Use for example like this:
public Exception CreateException([CallerLineNumber] int line = 0) => new Exception("This exception created at line " +line);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question