How to get Exception messages without the call stack

asp.net, c#, exception

Solution

You are not using the `Message` property here...

ex.ToString()

You need

ex.Message

Also, is this `Alert` only for your convenience? You should consider maybe having an error label on your screen, since the `pop-up` can always look messy.

EDIT: You should also look to catch more specific exceptions, instead of the catch all type of handling you have. Take a look at the possible exceptions in your try block, and accommodate them...for example...

catch (SoapException ex)
    {
         //handle
    }
catch (Exception e)
{
   //handle
}

Make sure the more specific exceptions come before the final `Exception` block.

Problem

I need to get only the exception message without the call stack or any other string. I thought that using `Exception.Message` would be enough, but it keeps giving me the message mixed with the call stack. Do you know how to get rid of all the rest of information that comes with `Exception.Message`? ``` try { } catch (Exception ex) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Message", "alert('" + ex.Message + "');", true); } ``` This is what I get when I use `ex.Message`: System.Web.Services.Protocols.SoapException: The server can not process the request. ---> System.NullReferenceException: Object reference not set to an instance of an object in . in WebService.ProcessRequestArc.............--- End of inner exception stack trace --- When what I only need is: The server can not process the request Is there any way to get only that part of the message?

Original source

Related problems