.Net Error Handling (get calling class/method)

Everyone eventually trys to write an all encompassing error handler that is easy to plug in their methods. Today, I'll provide you with a starting point for creating your own error handling class which will display the classes, method names, and error messages when an error occurs. This is by no means intended to be a complete solution for what to do with errors.

Everyone eventually trys to write an all encompassing error handler that
is easy to plug in their methods. Today, I'll provide you with a starting point
for creating your own error handling class which will display the classes,
method names, and error messages when an error occurs. This is by no
means intended to be a complete solution for what to do with errors.
It will just get started...

In .NET in the System.Diagnostics class, we can reference the Stack
to retrieve this information. The code below is a simple console
app demonstrating at a very basic level how it can be used.

Notice that I've set the main test class: Class1 to inherit the ErrorTrapper
class.

From there, you can plug in page redirection code (ASP.NET) or some
other custom notification process.

Class1.cs

using System;

namespace ErrHandler
{
class Class1 : ErrHandler.ErrorTrapper
{
[STAThread]
static void Main(string[] args)
{
ErrHandler.Class1 oTest = new ErrHandler.Class1();
oTest.Test1(5);
}

public void Test1(int nNum) { this.Test2(nNum); }
public void Test2(int nNum) { this.Test3(nNum); }
public void Test3(int nNum)
{

int nNewVal=0;
int nDivideBy=0;
this.ErrorNumber = 0;

// Purposely cause an error by dividing nNum by 0

try { nNewVal = nNum / nDivideBy; }
catch (Exception e) { this.SetOnError(e.Message); }

// your finally clause would go here. Perhaps you need to close
// db connections prior to leaving this page or section of the app.
// RedirectOnError() will do nothing if the ErrorNumber property of the
// ErrorTrapper class isn't set to something other than 0

this.RedirectOnError();
System.Console.ReadLine();

}
}
}



ErrorTrapper.cs

using System;
using System.Diagnostics;
using System.Text;

namespace ErrHandler
{

public class ErrorTrapper
{

public int ErrorNumber;
public string ErrorMessage;

public void SetOnError(string sMsg)
{

int nStackCnt=0;

StringBuilder oString = new StringBuilder();

StackTrace oStack = new StackTrace(true);

nStackCnt = oStack.FrameCount;

// I've decided to only show the last few items of the stack.
// They are displayed in order by most recent frame.

if (nStackCnt > 5) { nStackCnt = 5; }

for(int i = 0;i<nStackCnt;i++)
{
// The first frame (0) is always this method (SetOnError),
// so we don't want to display it.

if (i>0)
{
oString.Append(oStack.GetFrame(i).GetMethod().ReflectedType.FullName + "\n");
oString.Append(" " + oStack.GetFrame(i).GetMethod().ToString() + "\n");

// If it is the first frame we want to view, it will contain info from the
// class/method that generated the error

if (i==1) { oString.Append(" " + sMsg + "\n\n"); }
}
}

// Flag our class to react in RedirectOnError when it is called.

this.ErrorNumber = 2;
this.ErrorMessage = oString.ToString();

}

public void RedirectOnError()
{
if (this.ErrorNumber == 0) { return; }
// If an error exists, react with your custom page/notification process.
System.Console.WriteLine(this.ErrorMessage);
}

}



Submission Date:  9/23/2005 2:53:47 PM
Submitted By:  Robbe Morris
My Home Page:  http://www.robbemorris.com

By Robbe Morris   Popularity  (362 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP