Discovering exception thrown by a method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all,

I would like to know if there's a way to discover what exception(s) is
thrown by a function. I can imagine it is possible using reflection, but I
cannot find it.

For example:

toto instance of class Toto.

toto.myFunction(); // inside myFunction some exception are thrown, this is
what I try to figure out...

Thanks in advance.

Patrice

Thanks in advance.

Patrice
 
Hi,

Unfortunately there is no reflection method to identifying the exceptions
possibly raised by a method.
To the best of my knowledge, this would require disassembly of the IL to
identify exceptions raised explicity in the method.
 
pat said:
Hello all,

I would like to know if there's a way to discover what exception(s) is
thrown by a function. I can imagine it is possible using reflection, but I
cannot find it.

For example:

toto instance of class Toto.

toto.myFunction(); // inside myFunction some exception are thrown, this is
what I try to figure out...

Thanks in advance.

Patrice

Thanks in advance.

Patrice

If you are doing this for debugging purposes you can simply use the
ToString method of the exception.

try
{
toto.myFunction();
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}

The message box will display a message where you can see what type of
exception was thrown.

You can however catch a specific exception like so.

try
{
toto.myFunction();
}
catch(NullRefferenceException ex)
{
//NullRefferenceException exceptions will be caught here
}
catch(SomeOtherException ex)
{
//SomeOtherException exceptions will be caught here
}
catch(Exception ex)
{
//the rest of the exceptions get caught here
}

Hope this helps.
Nick Z.
 
Hi Patrice,

Here's a set of functions I wrote for dissecting Exceptions. It is far from
complete, in that it only specifically covers about 6 Exception types, and
treats all others as System.Exception. However, it does get the type name of
the exception, the exception message, source, and details about the
Exception, and calls itself recursively to drill down through all (if any)
InnerExceptions inside the Exception. It returns all this information as a
single string with line breaks delimiting the various details. I use it to
log Exceptions, and to send emails about Exceptions. Also note, these
methods are designed for .Net Platform 2.0. If you want to use them with
earlier versions, you may need to tweak it a bit (e.g. some of the Exception
classes don't exist in earlier platforms):

/// <summary>
/// Gets Detailed Information about an Exception
/// </summary>
/// <param name="ex"><c>System.Exception</c></param>
/// <returns>Formatted Detailed Exception Informatioon</returns>
public static string GetExceptionDetails(Exception ex)
{
StringBuilder sb = new StringBuilder("");
GetExceptionDetails(ex, ref sb);
return sb.ToString();
}

/// <summary>
/// Gets Detailed Information about an Exception
/// </summary>
/// <param name="ex"><c>System.Exception</c></param>
/// <param name="sb"><c>System.Text.StringBuilder</c> passed by
reference
/// to other methods and instances of this method, for
/// building the return value</param>
/// <returns>Formatted Detailed Exception Information</returns>
private static void GetExceptionDetails(Exception ex, ref
StringBuilder sb)
{
int i;
sb.Append(sb.Length == 0 ? "" : nl);
Type t = ex.GetType();
Type[] types = new Type[] {
typeof(WebException),
typeof(SqlException),
typeof(SmtpException),
typeof(SmtpFailedRecipientException),
typeof(SmtpFailedRecipientsException),
typeof(System.Configuration.ConfigurationException)};

string TypeName = t.ToString();

// Get type of Exception
for (i = 0; i < types.Length; i++)
{
if (t.Equals(types))
break;
}
sb.Append("Exception of Type \"" + TypeName + "\"" + nl);

// Handle with the appropriate handler
switch (i)
{
case 0: // WebException
GetWebException((WebException)ex, ref sb);
break;
case 1: // SqlException
GetSqlException((SqlException)ex, ref sb);
break;
case 2: // SmtpException
GetSmtpException((SmtpException)ex, ref sb);
break;
case 3: // SmtpFailedRecipientException
GetSmtpFailedRecipientException(
(SmtpFailedRecipientException)ex, ref sb);
break;
case 4: // SmtpFailedRecipientsException
GetSmtpFailedRecipientsException(
(SmtpFailedRecipientsException)ex, ref sb);
break;
case 5: // System.Configuration.ConfigurationException
GetConfigurationException(
(System.Configuration.ConfigurationException)ex, ref
sb);
break;
default:
GetSystemException(ex, ref sb);
break;
}
if (ex.Data != null && ex.Data.Count > 0)
{
sb.Append(nl + "Data:");
foreach (DictionaryEntry d in ex.Data)
sb.Append(nl + "Key: " + d.Key.ToString() +
", Value: " + d.Value.ToString());
}

if (ex.InnerException != null)
{
sb.Append(nl + "InnerException: " + nl);
GetExceptionDetails(ex.InnerException, ref sb);
}
sb.Append(nl + "StackTrace: " + nl + ex.StackTrace);
if (ex.HelpLink != null && ex.HelpLink != "")
sb.Append(nl + "HelpLink: " + ex.HelpLink);
}

/// <summary>
/// Get Details of a System.Exception
/// </summary>
/// <param name="ex"><c>System.Exception</c></param>
/// <param name="sb">reference to a <c>System.Text.StringBuilder</c>
/// building the Exception string</param>
public static void GetSystemException(Exception ex, ref StringBuilder sb)
{
sb.Append(nl + nl + ex.Message);
if (ex.Source != null)
sb.Append(nl + "Source: " + ex.Source);
}

/// <summary>
/// Get Details of a SqlException
/// </summary>
/// <param
name="ex"><c>System.Data.SqlClient.SqlException</c></param>
/// <param name="sb">reference to a <c>System.Text.StringBuilder</c>
/// building the Exception string</param>
public static void GetSqlException(SqlException ex, ref
StringBuilder sb)
{
int i;
sb.Append(nl + nl + "ErrorCode: " + ex.ErrorCode.ToString());
sb.Append(nl + "Line Number " + ex.LineNumber.ToString());
sb.Append(nl + "Error Number " + ex.Number.ToString());
sb.Append(nl +"Procedure Name: " + ex.Procedure);
sb.Append(nl + ex.Message);
if (ex.Errors.Count > 0)
{
sb.Append("nl + Errors: " + nl);
for (i = 0; i < ex.Errors.Count; i++)
{
sb.Append(nl + "Line Number " + ex.Errors.LineNumber.ToString());
sb.Append(nl + "Error Number " + ex.Errors.Number.ToString());
sb.Append(nl + "Procedure Name: " + ex.Errors.Procedure);
sb.Append(nl + ex.Errors.Message);
}
}
}

/// <summary>
/// Gets Details of a WebException
/// </summary>
/// <param name="ex"><c>System.Net.WebException</c></param>
/// <returns>String containing exception details</returns>
public static string GetWebException(WebException ex)
{
StringBuilder sb = new StringBuilder("");
GetWebException(ex, ref sb);
return sb.ToString();
}

/// <summary>
/// Get Details of a SqlException
/// </summary>
/// <param
name="ex"><c>System.Data.SqlClient.SqlException</c></param>
public static string GetSqlException(SqlException ex)
{
StringBuilder sb = new StringBuilder("");
GetSqlException(ex, ref sb);
return sb.ToString();
}

/// <summary>
/// Get Details of a WebException
/// </summary>
/// <param name="ex"><c>System.Net.WebException</c></param>
/// <param name="sb">reference to a <c>System.Text.StringBuilder</c>
/// building the Exception string</param>
private static void GetWebException(WebException ex, ref
StringBuilder sb)
{
sb.Append(nl + nl + ex.Message);
sb.Append(nl + ex.Response.ResponseUri.ToString());
sb.Append(nl + "Status: " + ex.Status.ToString());
}

/// <summary>
/// Get Details of a SmtpException
/// </summary>
/// <param name="ex"><c>System.Net.Mail.SmtpException</c></param>
/// <param name="sb">reference to a <c>System.Text.StringBuilder</c>
/// building the Exception string</param>
private static void GetSmtpException(SmtpException ex, ref
StringBuilder sb)
{
sb.Append(nl + nl + ex.Message);
sb.Append(nl + ex.StatusCode.ToString());
}

/// <summary>
/// Get Details of a ConfigurationException
/// </summary>
/// <param
name="ex"><c>System.Configuration.ConfigurationException</c></param>
/// <param name="sb">reference to a <c>System.Text.StringBuilder</c>
/// building the Exception string</param>
private static void GetConfigurationException(
System.Configuration.ConfigurationException ex, ref
StringBuilder sb)
{
sb.Append(nl + ex.BareMessage);
sb.Append(nl + "Details:" + nl + ex.Message);
sb.Append(nl + "FileName: " + ex.Filename);
sb.Append(nl + "Line Number: " + ex.Line.ToString());
}

/// <summary>
/// Get Details of a SmtpFailedRecipientException
/// </summary>
/// <param
name="ex"><c>System.Net.Mail.SmtpFailedRecipientException</c></param>
/// <param name="sb">reference to StringBuilder building the Exception
string</param>
private static void
GetSmtpFailedRecipientException(SmtpFailedRecipientException ex, ref
StringBuilder sb)
{
sb.Append(nl + nl + ex.Message);
sb.Append(nl + ex.StatusCode.ToString());
sb.Append(nl + ex.FailedRecipient);
}

/// <summary>
/// Get Details of a SmtpFailedRecipientsException
/// </summary>
/// <param
name="ex"><c>System.Net.Mail.SmtpFailedRecipientsException</c>c></param>
/// <param name="sb">reference to StringBuilder building the Exception
string</param>
private static void
GetSmtpFailedRecipientsException(SmtpFailedRecipientsException ex, ref
StringBuilder sb)
{
sb.Append(nl + nl + ex.Message);
sb.Append(nl + ex.StatusCode.ToString());
for (int i = 0; i < ex.FailedRecipient.Length; i++)
sb.Append(nl + ex.FailedRecipient);
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
sb.Append(nl + "InnerException[" + i.ToString() + "] -");
sb.Append(nl + GetExceptionDetails(ex.InnerExceptions));
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
I'd rather be a hammer than a nail.
 
Nope. This is just in the documentation. Else you'll have to decompile the
code.

Not aware of the details for this design but there's likely here a
"phylosophical" difference compared with Java.
 
Back
Top