Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Exception messages without the call stack

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?

like image 718
eddy Avatar asked Dec 07 '13 00:12

eddy


People also ask

How can I get only messages from exception?

The getMessage() method of Throwable class is used to return a detailed message of the Throwable object which can also be null. One can use this method to get the detail message of exception as a string value. Return Value: This method returns the detailed message of this Throwable instance. // the getMessage() Method.

Does exception message include stack trace?

Usually, a stack trace is shown when an Exception is not handled correctly in code. (An exception is what a runtime environment uses to tell you that there's an error in your code.) This may be one of the built-in Exception types, or a custom Exception created by a program or a library.

How do I get only the exception message in Python?

To catch and print an exception that occurred in a code snippet, wrap it in an indented try block, followed by the command "except Exception as e" that catches the exception and saves its error message in string variable e . You can now print the error message with "print(e)" or use it for further processing.


2 Answers

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.

like image 59
Christian Phillips Avatar answered Oct 25 '22 05:10

Christian Phillips


Exception.Message is correct for generic Exceptions.

Check out the more detailed info available for SoapException

Here is an example:

namespace ExceptionHandlingTestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("do something");
            throw new System.Web.Services.Protocols.SoapException();
            //throw new Exception("my exception", new Exception("my inner exception"));
        }
        catch (System.Web.Services.Protocols.SoapException soapEx)
        {
            Console.Write("Detail: ");
            Console.WriteLine(soapEx.Detail);
            Console.Write("Node: ");
            Console.WriteLine(soapEx.Node);
            Console.Write("Role: ");
            Console.WriteLine(soapEx.Role);
            Console.Write("Message: ");
            Console.WriteLine(soapEx.Message);
        }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (ex.InnerException != null)
                {
                    Console.WriteLine("inner exception msg: " + ex.InnerException.Message);
                }
            }
            Console.ReadKey();
        }

    }
}
like image 35
Chad Dienhart Avatar answered Oct 25 '22 04:10

Chad Dienhart