SqlDeveloper
SqlDeveloper

Reputation: 1

How to track which line raised the exception

try
{
    //This code BlockLine no 1Line no 2Line no 3etc
}
catch (Exception ex) 
{
    LblError.Text= ex.Message + ex.InnerException.Message
}

I have a code with a try catch block. The try block has more than one line of code. I would like to know which line present within the try block is raising the exception.

Upvotes: 0

Views: 63

Answers (2)

Brian Rodgers
Brian Rodgers

Reputation: 81

( using System.Diagnostics; )

    int lineNumber = (new StackTrace(ex, true)).GetFrame(0).GetFileLineNumber();
    var fileName = (new StackTrace(ex, true)).GetFrame(0).GetFileName();

Should give you where the problem started.

Upvotes: 0

Serame Simon
Serame Simon

Reputation: 71

You will need to look at the Stack Trace of the exception to track the line that throws the exception.

however if you want to get the full details about the exception use ToString method

Upvotes: 5

Related Questions