Viewing Exception Details

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Magnus.Moraberg@gmail.com

    Viewing Exception Details

    Hi,

    Is it a code practice for me to have writen the following Class? -

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Windows. Forms;
    using System.Collecti ons;

    namespace mine
    {
    static class ExceptionDetail s
    {
    static public string Details(Excepti on exception)
    {
    string exceptionString = "";

    try
    {
    int i = 0;
    while (exception != null)
    {
    exceptionString += "*** Exception Level " + i + "
    ***\n";
    exceptionString += "Message: " + exception.Messa ge
    + "\n";
    exceptionString += "Source: " + exception.Sourc e +
    "\n";
    exceptionString += "Target Site: " +
    exception.Targe tSite + "\n";
    exceptionString += "Stack Trace: " +
    exception.Stack Trace + "\n";
    exceptionString += "Data: ";
    foreach (DictionaryEntr y keyValuePair in
    exception.Data)
    exceptionString += keyValuePair.Ke y.ToString()
    + ":" + keyValuePair.Va lue.ToString();
    exceptionString += "\n***\n\n" ;

    exception = exception.Inner Exception;

    i++;
    }
    }
    catch{}

    return exceptionString ;
    }
    }
    }

    I then use it as follows -

    try
    {
    ...
    }
    catch (Exception exception)
    {
    MessageBox.Show (ExceptionDetai ls.Details(exce ption), "Error.");
    }

    Thanks,

    Barry.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Viewing Exception Details

    Well, if you want to show the details, and this is what you want to see,
    then by all means, use it. I'd probably move the call to MessageBox.Show
    into the method as well (save some typing) and call it something else (like
    DisplayDetails) .

    However, if you want to use this for other purposes (like logging to a
    file) then it is just fine the way it is (although there are other logging
    mechanisms out there).


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    <Magnus.Moraber g@gmail.comwrot e in message
    news:08261222-98d7-40a1-92c0-da279a6b8052@35 g2000pry.google groups.com...
    Hi,
    >
    Is it a code practice for me to have writen the following Class? -
    >
    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Windows. Forms;
    using System.Collecti ons;
    >
    namespace mine
    {
    static class ExceptionDetail s
    {
    static public string Details(Excepti on exception)
    {
    string exceptionString = "";
    >
    try
    {
    int i = 0;
    while (exception != null)
    {
    exceptionString += "*** Exception Level " + i + "
    ***\n";
    exceptionString += "Message: " + exception.Messa ge
    + "\n";
    exceptionString += "Source: " + exception.Sourc e +
    "\n";
    exceptionString += "Target Site: " +
    exception.Targe tSite + "\n";
    exceptionString += "Stack Trace: " +
    exception.Stack Trace + "\n";
    exceptionString += "Data: ";
    foreach (DictionaryEntr y keyValuePair in
    exception.Data)
    exceptionString += keyValuePair.Ke y.ToString()
    + ":" + keyValuePair.Va lue.ToString();
    exceptionString += "\n***\n\n" ;
    >
    exception = exception.Inner Exception;
    >
    i++;
    }
    }
    catch{}
    >
    return exceptionString ;
    }
    }
    }
    >
    I then use it as follows -
    >
    try
    {
    ...
    }
    catch (Exception exception)
    {
    MessageBox.Show (ExceptionDetai ls.Details(exce ption), "Error.");
    }
    >
    Thanks,
    >
    Barry.

    Comment

    Working...