Please note that Logging is used as an example for the catch block processing just to demonstrate the concept; but it could equally apply to any call inside the catch block that can throw exceptions, for any emaling of errors which may also be done inside the catch block.
[C#]
// ----------------------------------------------
// From: http://coding-help.blogspot.com/
// Title: Error Handling
// Date: 12 July 2007
// Author: R. K. Wijayaratne
// Description: Shows a good way to handle
// exceptions; that is try to log it and then
// swallow it if it succeeds in logging it,
// but otherwise throw it up to the caller.
//
// Please note that Logging is used as an
// example for the catch block processing
// just to demonstrate the concept; but it
// could equally apply to any call inside the
// catch block that can throw exceptions,
// e.g. emailing of errors which may also
// be done inside the catch block.
//
// To Run: Create a new C# file, e.g. "Program.cs",
// copy and paste the code below into it and save.
// To compile it open the .NET Framework command
// prompt and type:
//
// csc C:\MyPath\Program.cs
//
// where "C:\MyPath\" is the path of the C# file.
// If there are any errors correct them. To run
// the program type "Program" and press "Enter."
// ----------------------------------------------
using System;
using System.Diagnostics;
public class TestHarness
{
// Program entry point.
static void Main(string[] args)
{
// Start running the test.
StartTest();
}
// Test driver method.
public static void StartTest()
{
try
{
// Test error handling.
TestCase.TestErrorHandling();
}
catch (Exception exc)
{
// Write errors to console.
Console.Write(exc.ToString());
}
}
}
public class TestCase
{
public static void TestErrorHandling()
{
try
{
// Try to do some work.
// Doing some work...
// Simulate error.
throw new InvalidOperationException();
}
catch (InvalidOperationException invOpExc)
{
string errDesc = "An invalid operation"
+ " occurred while doing some work.";
string extraErrs = null;
bool handled = true;
try
{
// Try to log original error.
EventLog.WriteEntry(
"Program",
errDesc+"\n\n"+invOpExc.ToString(),
EventLogEntryType.Error);
// Simulate new error during logging.
throw new ArgumentException();
}
catch (ArgumentException argExc)
{
handled = false;
extraErrs += "\n\nAdditional error"
+ " occurred while"
+ " logging:\n\n"
+ argExc.ToString();
}
// Throw it up to caller if not handled
// and also append any logging errors.
if (!handled)
{
// Append logging errors.
errDesc = string.Format("{0}\n====="
+ "{1}\n\n=====\n",
errDesc, extraErrs);
throw new Exception(errDesc, invOpExc);
}
//else // Implicit.
//{
// Logging and other corrective
// measures were successful
// and the error was handled
// so swallow the error.
//}
}
finally // Optional.
{
// Always gets called so do any clean-up
// here such closing database connections
// and releasing other valuable resources.
}
}
}
Console Output
System.Exception: An invalid operation occurred while doing some work.
=====
Additional error occurred while logging:
System.ArgumentException: Value does not fall within the expected range.
at TestCase.TestErrorHandling()
=====
---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at TestCase.TestErrorHandling()
--- End of inner exception stack trace ---
at TestCase.TestErrorHandling()
at TestHarness.StartTest()
Also See
- See 'Exception' (on MSDN) here http://msdn2.microsoft.com/en-us/library/5b2yeyab.aspx
- See 'Catch Block Error Handling' here http://coding-help.blogspot.com/2007/07/catch-block-error-handling.html