The Use Of InnerException

An unknown error withan unknown description has occurred. Please remain calm while the appropriateauthorities are on their way.
Do you recognize these kind oferrors? Ever wondered how you can manage your errors in a way you can track 'mdown from their source to the point you need to show them? The solution issimple. Use the innerexception from the standard ApplicationExceptionobject. In the next example I use a custom Exception because i need someadditional info but it will also work finewith a default Exception.

STEP1: Create my own exception class wichcontains extra properties (sender and details). This class also contains aShowError method that loads a form that will show the error message(s). TheToString is overridden so it can show my extra added information. 

//——————————————————————-
//
// Author : Loek van den Ouweland
// EMail  : lvdo@in2net.nl
// Creation date : July 2003
//
//——————————————————————-
using System;
using System.Windows.Forms;

namespace LootAtThisError
{
    public class LookException : ApplicationException
    {
        private string mSender;
        private string mDetails;
        publicLookException (string message, Exception ex, string sender, string details) : base (message,ex)
        {
            mSender=sender;
            mDetails=details;
        } 
        public string Sender
        {
            get{return mSender;}
        }
        public string Details
        {
            get{return mDetails;}
        }
             
        public void ShowError()
        {
            FExceptionfe=new FException(this);
            fe.ShowDialog();
        }

        public override string ToString()
        {
            string errortext="Message: " + this.Message + Environment.NewLine +
            "Sender : " + mSender + Environment.NewLine +
            "Details: " + mDetails + Environment.NewLine;

            return errortext;
        } 
    }

}

STEP2: The first error that will appear isalways of type: Exception. Just catch this bad boy and throw your own:

stringsql="this will certainly raise an arror";
.
.
catch
(Exception ex)
{
   thrownewLookException (ex.Message,ex,ex.Source,sql);
}

STEP3: Just catch and rethrow your error asmany times as you want. These are LookExceptions:

catch(LookException ex)
{
  
thrownewLookException ("myfriendly error message",ex,"it happened here","extra detailslike ID's");
}

STEP4: At last, i.e. in your presentationlayer, just catch the LookExceptionand show the error:

catch (LookExceptionex)
{
    ex.ShowError();
}

catch(Exception ex)
{
  // also: always catch a default Exception just to make sure no-oneescapes!
}

STEP5: Create a form that's capable of showingthe error. To show the error with all it's innerexceptions, pass the exceptionto the form (store it in mException) and do something like this:

private voidFException_Load(object sender,System.EventArgs e)
{
    txtMessage.Text=mException.Message;
    Exception inex=mException;
    while (inex!=null)
  
{
  
     txtDetails.Text+=inex.ToString() + Environment.NewLine;
  
     txtDetails.Text+=Environment.NewLine + "<*==—————————==*>" +Environment.NewLine;
       inex=inex.InnerException;
  
}
}

CONCLUSION: The trick here is that every timeyou throw an error, you can pass the 'just catched' error along with it. Finallyshow the exception plus all innerexceptions by calling their ToString-functions.Happy catching!

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!