Click here to Skip to main content
15,899,126 members
Home / Discussions / C#
   

C#

 
GeneralRe: Moved from Q/A (originally posted by dmpinusa): Centralizing Exception Handling Code Pin
dpminusa20-Nov-10 14:14
dpminusa20-Nov-10 14:14 
AnswerRe: Moved from Q/A (originally posted by dmpinusa): Centralizing Exception Handling Code Pin
_Erik_19-Nov-10 3:46
_Erik_19-Nov-10 3:46 
GeneralRe: Moved from Q/A (originally posted by dmpinusa): Centralizing Exception Handling Code Pin
dpminusa20-Nov-10 14:18
dpminusa20-Nov-10 14:18 
GeneralRe: Moved from Q/A (originally posted by dmpinusa): Centralizing Exception Handling Code Pin
_Erik_20-Nov-10 16:01
_Erik_20-Nov-10 16:01 
GeneralRe: Moved from Q/A (originally posted by dmpinusa): Centralizing Exception Handling Code Pin
dpminusa20-Nov-10 20:01
dpminusa20-Nov-10 20:01 
GeneralRe: Moved from Q/A (originally posted by dmpinusa): Centralizing Exception Handling Code Pin
_Erik_20-Nov-10 23:21
_Erik_20-Nov-10 23:21 
GeneralRe: Moved from Q/A (originally posted by dmpinusa): Centralizing Exception Handling Code Pin
dpminusa21-Nov-10 4:46
dpminusa21-Nov-10 4:46 
AnswerRe: Moved from Q/A (originally posted by dmpinusa): Centralizing Exception Handling Code Pin
Rhys Gravell21-Nov-10 23:57
professionalRhys Gravell21-Nov-10 23:57 
I always think of Exception Handling as a two step process;

1. There are going to be exceptional circumstances under which errors occur, which, due to good analysis, testing or experience can be handled at a reasonably low level. I usually consider database access as good example of this as the availability of the database should be considered outside of the scope of control of any DAL, (Data Access Layer), BLL, (Business Logic Layer), or UI.

2. In a project of any size there is usually going to be unpredictable responses to certain actions, some of which will generate exceptions and the reality is you simply cannot, (and should not), attempt to protect every method, event handler etc. within an Exception Handler. However, should an unpredicted, or unpredictable exception occur an application should handle it gracefully and take appropriate action based on the exception type, which as a programmer it is your job to consider.

My approach is typically to protect more sensitive and/or predictable area's of code with explicit exception handlers, but then to also attempt to protect an application at run time so that anything exceptional/unpredictable occurring is logged appropriately and potentially reported to the end user. The granular try...catch approach is obviously self explanatory, but the high level approach I normally undertake with an ExceptionManagement class which when instantiated subscribes to the '' and '' events and which itself can be configured at instantiation to tog or report handles exceptions in a predefined manner;

public class ExceptionManager : IDisposable
{
    // Some code...

    public ExceptionLogger()
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
    }

    // Some more code...
}

static class Program
{	
	internal const string C_ExecutingApplicationName = "My Application Name";

	// private members
	private static Form mainForm;

	[STAThread]
	static void Main()
	{
		Application.EnableVisualStyles();
		Application.SetCompatibleTextRenderingDefault(false);
		AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);

		// Set up a catch all error handler in case we have an error in code where we don't have a specific handler
		ExceptionManager exceptionManager = ExceptionManager();

		// Set up a catch all error handler in case we have an error in code where we don't have a specific handler
		ExceptionManager exceptionManager = new ExceptionManager();
		// Configure Exception manager, i.e., 
		// exceptionManager.NotificationType = Inform;
		// or 
		// exceptionManager.NotificationType = ApplicationEventLog;
		// or 
		// exceptionManager.NotificationType = DatabaseLog;
		// or 
		// exceptionManager.NotificationType = All;
		// etc. with supporting code with the ExceptionManager class

		// Run it...
		mainForm = new Forms.Main();
		Application.Run(mainForm);
	}
}

Rhys

"With no power comes no responsibility"

Questiondatetimepicker with multiple selected dates Pin
ajithnamboodiri18-Nov-10 19:58
ajithnamboodiri18-Nov-10 19:58 
AnswerRe: datetimepicker with multiple selected dates Pin
Mycroft Holmes18-Nov-10 20:46
professionalMycroft Holmes18-Nov-10 20:46 
AnswerRe: datetimepicker with multiple selected dates Pin
RobCroll19-Nov-10 0:55
RobCroll19-Nov-10 0:55 
AnswerRe: datetimepicker with multiple selected dates Pin
Gregory Gadow19-Nov-10 9:40
Gregory Gadow19-Nov-10 9:40 
AnswerRe: datetimepicker with multiple selected dates Pin
thatraja20-Nov-10 2:13
professionalthatraja20-Nov-10 2:13 
QuestionPrint a data using GDI object in windows form Pin
ragupathi.p18-Nov-10 17:55
ragupathi.p18-Nov-10 17:55 
AnswerRe: Print a data using GDI object in windows form Pin
Luc Pattyn18-Nov-10 18:26
sitebuilderLuc Pattyn18-Nov-10 18:26 
GeneralRe: Print a data using GDI object in windows form Pin
ragupathi.p22-Nov-10 19:24
ragupathi.p22-Nov-10 19:24 
QuestionVersioning Question Pin
Richard Andrew x6418-Nov-10 13:51
professionalRichard Andrew x6418-Nov-10 13:51 
AnswerRe: Versioning Question PinPopular
Luc Pattyn18-Nov-10 14:14
sitebuilderLuc Pattyn18-Nov-10 14:14 
GeneralRe: Versioning Question Pin
Richard Andrew x6418-Nov-10 14:18
professionalRichard Andrew x6418-Nov-10 14:18 
GeneralRe: Versioning Question Pin
Luc Pattyn18-Nov-10 14:32
sitebuilderLuc Pattyn18-Nov-10 14:32 
AnswerRe: Versioning Question Pin
Dave Kreskowiak18-Nov-10 15:25
mveDave Kreskowiak18-Nov-10 15:25 
GeneralRe: Versioning Question Pin
Richard Andrew x6418-Nov-10 15:29
professionalRichard Andrew x6418-Nov-10 15:29 
AnswerRe: Versioning Question Pin
Bernhard Hiller21-Nov-10 23:22
Bernhard Hiller21-Nov-10 23:22 
Questiongetting data over website Pin
Erdinc2717-Nov-10 21:51
Erdinc2717-Nov-10 21:51 
AnswerRe: getting data over website Pin
Pete O'Hanlon17-Nov-10 23:10
mvePete O'Hanlon17-Nov-10 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.