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?
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.
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.
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.
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.
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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With