.Net Code and Better Exception Handling

Background

Earlier today I found myself looking an error message in the eye and wishing I had more.

Error Message

Here is the error message

Exception is The given ColumnMapping does not match up with any column in the source or destination.

 

 

Code

using System;
using System.Xml;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data;
using System.Data.OleDb;
using System.Management;
using System.Data.SqlClient;



public class exceptionNode
{
	public Exception obj { get; set; }

    public string Message { get; set; }	
	  
}

public class exceptionHandler
{
  
  private ArrayList objErrList = new ArrayList();
  
  private String strErrorLog = null;

  private OleDbException objExOLEDB = null;
  
  private SqlException objExSQL = null;

			
  static string getClassName()
  {

		return (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);
		
  }	  
  
  
  static string getMethodName()
  {
  
		String strMethodName = null;

		System.Diagnostics.StackFrame stackFrame
			= new System.Diagnostics.StackFrame(1, false);
				
		System.Reflection.MethodBase method 
			= stackFrame.GetMethod();
			
		strMethodName = method.Name;			
		
		return (strMethodName);
		
  }  


  public void processExceptionList(Exception ex)
  {

    exceptionNode objExNode = null;
	Exception     exNext = null;
	
	try
	{
		//clear collection
		objErrList.Clear();
		
		//if exception is null, do nothing
		if (ex == null)
		{
			
			return;
			
		}
		
		//get current node
		objExNode = new exceptionNode();
		
		objExNode.obj = ex;
		
		if (ex is OleDbException)
		{
			
			objExOLEDB = (OleDbException) ex;
			
			objExNode.Message 
				
				= 
				  "Exception type is "
				+ ex.GetType().Name
				+ ".  "
				//+ Environment.NewLine 					  
				+ "Message: " + objExOLEDB.Message
				+ Environment.NewLine 	
				//+ "NativeError: " + objExOLEDB.NativeError
				+ Environment.NewLine 					
			    + "Source: " + objExOLEDB.Source
				+ Environment.NewLine 					
			    //+ "SQLState: " + objExOLEDB.SQLState
			    + "Error Code: " + objExOLEDB.ErrorCode
				+ Environment.NewLine 					
				;
		}
		if (ex is SqlException)
		{
			
			objExSQL = (SqlException) ex;
			
			objExNode.Message 
				
				= 
				  "Exception type is " 
				+ ex.GetType().Name				  
				//+ Environment.NewLine 				
				+ ".  "				
				+ "Message: " + objExSQL.Message
				+ Environment.NewLine 	
				//+ "NativeError: " + objExOLEDB.NativeError
				+ Environment.NewLine 					
			    + "Source: " + objExSQL.Source
				+ Environment.NewLine 					
			    //+ "SQLState: " + objExOLEDB.SQLState
			    + "Error Code: " + objExSQL.ErrorCode
				+ Environment.NewLine 					
			    + "Number: " + objExSQL.Number
				+ Environment.NewLine 													
			    + "State: " + objExSQL.State
				+ Environment.NewLine 									
				;
		}		
		else
		{	
			objExNode.Message 
				=  "Exception Type is "
					 + ex.GetType().Name				
					 + ".  "
					 + "Message is " 
					 + ex.Message
					;
		}	
		
		objErrList.Add(objExNode);
		
							
		//get next exception
		exNext = ex.InnerException;
		
		//successively add next nodes		
		while (exNext != null)
		{
			objExNode = new exceptionNode();
				
			objExNode.obj = exNext;
			
			objExNode.Message = "Exception type is " 
								+ exNext.GetType().Name	
								+ ".  "
								+ "Message is "  								
								+ exNext.Message
								;
			
			objErrList.Add(objExNode);
			
			exNext = exNext.InnerException;
			
		}							

	}
	catch (Exception exIn)
	{
		
		strErrorLog = "Exception in " 
							+ getClassName() 
							+ "." 
							+ getMethodName()
							+ ". "
							+ "Exception is "
							+ exIn.Message
							;
							
		//get current node
		objExNode = new exceptionNode();
		objExNode.obj = exIn;
		objExNode.Message = strErrorLog;
		objErrList.Add(objExNode);							
		
	}
	
  }   
  
	   
  public string exceptionAsString()
  {

	exceptionNode ex;
	String        strBuffer = "";
	int           iNumberofEx;
	
	if (objErrList == null)
	{		
		return ("");
	}
	
	iNumberofEx = objErrList.Count;
	
	for (int i=0; i < iNumberofEx; i++)
	{
		ex = (exceptionNode) objErrList[i];
		
		strBuffer = strBuffer 
						+ i
						+ ") "
						+ ex.Message
						+ Environment.NewLine 
						;
		
	}	  

	return strBuffer;	
	
  }
  
}

Fuller Error Message

Here is our fuller error message:

0) Exception Type is InvalidOperationException. Message is The given value of type Guid from the data source cannot be converted to type int of the specified target column.

1) Exception type is InvalidCastExceptionFailed to convert parameter value from a Guid to a Int32.

2) Exception type is InvalidCastExceptionObject must implement IConvertible.

 

SQL Server Profiler

BTW, I tried to see whether SQL Server Profiler will allow quicker access to the error, but no luck this time.

Listening

Listening to ….

Brad Paisley
Wish you would stay
Link

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s