E
E
estry2020-08-06 12:11:22
C++ / C#
estry, 2020-08-06 12:11:22

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

2 answer(s)
V
Vasily Bannikov, 2020-08-06
@estry

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);

But I don’t see the point at all, since the line number is written in the stack trace anyway, if the assembly is in debug

P
Peter, 2020-08-06
@petermzg

If C# were an Interpreted Programming Language, then such information would be available. But C# is a compiled language

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question