unhandled exception not being caught in asyncCallback's method

T

TS

The method that is envoked using an asynCallback to asynchronously call a
web method. I read the following article and implemented what it said:
http://msdn.microsoft.com/msdnmag/issues/04/06/NET/. It says that the
Application.ThreadException won't catch exceptions of other threads, so this
asyncCallback's exception would not be caught in this situation. It says
that i also have to catch the CLR's
AppDomain.CurrentDomain.UnhandledException. I implemented it, but the
exception is still not being caught. Can anyone tell my what i am doing
wrong?

The Loader class is the starting object that sets up all the event handling.
If you need the code for the asyncCallback method, let me know.

public class Loader

{

[STAThread]

public static void Main()

{

try

{

SubMain();

}

catch (Exception e)

{

HandleUnhandledException(e);

}

}

public static void SubMain()

{

// add the hook to catch all CLR unhandled exceptions (asyncronous method
calls, etc.)

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUnhandledException);

// add the hook to catch all windows forms' unhandled exceptions

Application.ThreadException+=new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Application.Run(new UIPStarter());

}

// CLR unhandled exception

private static void OnUnhandledException(Object sender,
UnhandledExceptionEventArgs e)

{

HandleUnhandledException(e);

}

// Windows Forms unhandled exception

private static void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)

{

HandleUnhandledException(e);

}

static void HandleUnhandledException(Object o)

{

Exception e = o as Exception;

BaseForm.ReportException(e);

MessageBox.Show("An unhandled exception occurred and the application is
shutting down.");

Application.Exit();

}

}

}
 
J

Jeffrey Tan[MSFT]

Hi TS,

Thanks for your posting!

Based on my understanding, you uses a winforma application to call a
webmethod from a remote webservice server. You want to catch the unhandled
exception thrown out by the webservice in an asynchronized call.

Have you downloaded the sample code snippet of that MSDN article and done
some test? Actually, it works well on my side. I also managed to modify its
code to asynchronized call a web method, which throws out a unhandled
exception. And the Application.ThreadException catched this webservice
exception.

The sample code I used to asynchronized call the web method is listed
below(Just modify the 5 buttons' handler in that MSDN article sample code):
private void OnButtonClick(Object sender, EventArgs args)
{
bool b=true;
exceptiontest.sha_dng_chn.Service1 obj=null;
IAsyncResult ia=null;
switch (((Button)sender).Text)
{
case "Throw From This Button's Event Handler":
throw new InvalidOperationException();

case "Close Window and Throw After the Fact":
obj.EndThrowExceptionMethod(ia);
break;

case "Create a Thread and Throw From It":
obj=new exceptiontest.sha_dng_chn.Service1();

ia=obj.BeginThrowExceptionMethod("abc", null, null);
ia.AsyncWaitHandle.WaitOne();

break;

case "Throw From a Thread-pool Thread":
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolMethod));
break;

case "Throw From a Finalizer Method":
CreateObjectThatThrowsOnFinalize();
GC.Collect();
GC.WaitForPendingFinalizers();
break;
}
}
If I misunderstand you, please feel free to tell me.
==============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top