How to Display Exception Message in Asp.Net


Exception Message is nothing but a error message it can raised when run program output through code behind then it will comes if we have any kind of error in our code. Here we will Check Exception Message, Stack trace, Source of that Message, Target Site with alert box through code behind.

Step 1: Drag and Drop Button Control in Webpage.

Step 2: Generate Click event and create try catch block so we can easily get exception while error raised.

protected void RaiseException(object sender, EventArgs e)
{
  try
  {
    int i = int.Parse("Umesh");
  }
  catch (Exception ex)
  {
    string message = string.Format("Message: {0}\\n\\n", ex.Message);
    message += string.Format("StackTrace: {0}\\n\\n", ex.StackTrace.Replace(Environment.NewLine, string.Empty));
    message += string.Format("Source: {0}\\n\\n", ex.Source.Replace(Environment.NewLine, string.Empty));
    message += string.Format("TargetSite: {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty));
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(\"" + message + "\");", true);
  }
}

Step 3: Now run in browser and click on button to generate exception message in alert box.

Related Posts

Previous
Next Post »

Thanks for comments.....