Reputation: 5933
Everyone is familiar with the default error handler for ASP.NET. The yellow boxes that contain Source Error (5 lines of code where the error happened) and Source File (filename and line number) like so:
Source Error:
Line 48: public ActionResult TriggerException()
Line 49: {
Line 50: throw new SystemException("This is a generated exception to test the global error handler.");
Line 51: }
Line 52:
Source File: c:\MyApp\Controllers\TestToolsController.cs Line: 50
I am building a custom error handler and want to get these same pieces of information, but they are not contained in the exception object. Does anyone know how I may retrieve these items.
Upvotes: 2
Views: 3418
Reputation: 33139
The line number is not available in the Exception itself, but it is available in the StackTrace, like so:
try
{
// code that throws an Exception here
}
catch (Exception exc)
{
var frame = new StackTrace(exc, true).GetFrame(0); // where the error originated
var lineNumber = frame.GetFileLineNumber();
// Handle line numbers etc. here
}
Upvotes: 6