0

I have a function with a try-catch block. When I run the solution on localhost, it doesn't throw an exception. But when I run it on the server, I sometimes get the exception message "Object reference not set to an instance of an object."

The catch block in my code looks like this:

catch (Exception e)
{
    Mailing.SendMail(@gmail.com,
        e.Message + Environment.NewLine + Request.UrlReferrer.OriginalString);
}

Whenever an exception is caught, I want to send an email.

My question is: can I get the trigger parameter or line from the exception?

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Yitzhak Weinberg
  • 2,324
  • 1
  • 17
  • 21

1 Answers1

0

Are you talking about obtaining data from exception?

  1. e.StackTrace - stack trace
  2. e.Message - user friendly info
  3. e.Data - additional data (may be null)

To get line number, you can use:

StackTrace st = new StackTrace(ex, true);
StackFrame frame = st.GetFrame(0);
int line = frame.GetFileLineNumber();
Fka
  • 6,044
  • 5
  • 42
  • 60
  • Exception.ToString() will return all of these. No need to call each individually – Panagiotis Kanavos Nov 24 '16 at 08:55
  • Yes, but if he wanted to use it somehow different, this is more flexible. – Fka Nov 24 '16 at 08:56
  • This is more work and prone to errors. If you want to log something, just log the exception. If you want to ask about exceptions in StackOverflow, just post `Exception.ToString()` – Panagiotis Kanavos Nov 24 '16 at 08:57
  • Also, this won't show any inner exceptions. Eg, tasks wrap actuall exceptions in an `AggregateException`, libraries typically wrap detailed exceptions in a high level exception. `Exception.ToString()` walks the entire exception tree – Panagiotis Kanavos Nov 24 '16 at 09:03