[How] to Display Exception Message in Asp.Net

Display Exception Message in Asp.Net,How to Display Exception Message in Asp.Net
Share it:

How to Display Exception Message in Asp.Net

How to Display Exception Message in Asp.Net

Description

In these articles, we learn How to Display Exception Message in Asp.Net, Exception Message is nothing but an error message it can raise when run program output through code behind then it will come 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 the Click event and create a try-catch block so we can easily get an 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 the browser and click on the button to generate an exception message in the alert box.
Share it:

aspnet

Post A Comment:

0 comments: