Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

The Use Of InnerException
By Loek van den Ouweland

An unknown error with an unknown description has occurred. Please remain calm while the appropriate authorities are on their way.
Do you recognize these kind of errors? Ever wondered how you can manage your errors in a way you can track 'm down from their source to the point you need to show them? The solution is simple. Use the innerexception from the standard ApplicationException object. In the next example I use a custom Exception because i need some additional info but it will also work fine with a default Exception.

STEP1: Create my own exception class wich contains extra properties (sender and details). This class also contains a ShowError method that loads a form that will show the error message(s). The ToString 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;
        public LookException (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()
        {
            FException fe=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 is always of type: Exception. Just catch this bad boy and throw your own:

string sql="this will certainly raise an arror";
.
.
catch
(Exception ex)
{
   throw new LookException (ex.Message,ex,ex.Source,sql);
}

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

catch (LookException ex)
{
  
throw new LookException ("my friendly error message",ex,"it happened here","extra details like ID's");
}

STEP4: At last, i.e. in your presentation layer, just catch the LookException and show the error:

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

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

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

private void FException_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 time you throw an error, you can pass the 'just catched' error along with it. Finally show the exception plus all innerexceptions by calling their ToString-functions. Happy catching!