Top Level Exception Handling with Event Handlers in C #
I am currently asking myself some questions about exception handling and event handlers, and I hope some of you will be of some help to me.
I'll start by explaining what I would like to do in my C # application:
I have a top level method (let's call it main method). This method calls an asynchronous method (called connect) that connects to the FTP server.
An EventHandler object is associated with this connection and a callback method is called when the connection is successful.
I want to handle exceptions that can be triggered during the whole process. So I would like to catch it in the top level method. It works great for exceptions thrown by the connect method (which is called inside the top level method). However, it does not work for exceptions thrown inside the callback method: the top-level method does not catch them and the execution is not executed.
What can I do to throw these exceptions with the top level method? I don't want to handle these exceptions in the callback.
a source to share
See how Backgroundworker works: Exception is propagated to the handler of the handled event.
I am assuming you have some sort of State object being passed to / from the delegate where you can add a property like this. And you have to catch all exceptions in the stream, at the very range. But "conversion" simply means passing it.
There is a standard template for the RunWorkerCompleted event, see this MSDN page .
a source to share
Consider the below code snippet to wrap all of your code in a global exception handler:
namespace MyClient
{
class Program
{
static void Main(string[] args)
{
try
{
bool isSuccess = SubMain(string[] args);
}
catch (Exception e)
{
HandleExceptionGracefully(e);
}
}
static bool SubMain(string[] agrs)
{
// Do something
}
static void HandleExceptionGracefully(Exception e)
{
// Display/Send the exception in a graceful manner to the user/admin.
}
}
}
Also, don't forget to make your error handling convenient.
a source to share
The application class has a class called ThreadException . This event will be fired whenever an exception is not thrown anywhere in the current call stack.
Edited:
Sorry I misunderstood the question - I didn't realize that the "main" method in your example is not the actual main method. In this case, you might want to catch the exception inside the callback, but not handle it - instead, just pass it back to the main method as part of the event arguments.
The BackgroundWorker in Winforms does something similar.
a source to share
Thank you for your responses.
It seems that using BackgroundWorker solves this problem.
I haven't tried it because I decided to avoid this implementation burden. So I took my asynchronous call and made the application run synchronously.
One tip for people using Compact Framework instead of the full .NET Framework: BackgroundWorker is not available in CF, but a similar solution is provided by OpenNETCF (see BackgroundWorker class in Smart Device Framework).
a source to share
A more convenient way to deal with this top-level exception handling problem is to use delegates.
These C # delegates allow you to call methods in an asynchronous manner. And delegates allow top-level exception handling as well. Indeed, exceptions thrown inside delegates are re-loaded into the original stream.
I don't know why I haven't thought about delegates before. Hope this helps.
See these interesting articles about delegates:
a source to share