none
Custome Exception - not able to catch RRS feed

  • Question

  •  

    Hi All,

     

    I am creating a cutom exception in program and trying to catch in either the CurrentDomain_UnhandledException or Application_ThreadException events. 

     

     

    I have two different problems:

    Case 1:

    The custom exception is thrown from the OnCellPaintEvent.  

    In this case I cannot get the exception to be caught in either of the top level exception handlers instead it just seems to disappear.  If I let the system exception be thrown from the OnCellPaintEvent it gets caught in the  Application_ThreadException. 

     

    Case 2:

    From the background_RunWorkerCompleted method I throw a custom exception.  It does get caught in the Application_ThreadException but instead of being my custom exception I am getting the original system exception.

     

    This seems to be a problem with threading and events but I have researched and cannot seem to find how to throw custom exceptions from threads/events to be caught at the top level to be handled.

     

    Any ideas on what I am doing wrong would be greatly appreaciated.

     

    Thanks In Advance

    kareno12

    Monday, February 11, 2008 10:21 PM

Answers

  • I assume you're talking about .net 2.0 and above.

    AppDomain.UnhandledException is just a notifyer of an exception in the current AppDomain, it is not a exception handler like the catch block, it won't swallow the exception. The exception will cause the whole application down. You can get a chance to do some logging option in the event handler, but you cannot stop the application to be shut down by CLR.

    Application.ThreadException can only capture and hanlde the exception thrown in Windows Forms threads, though the exceptions thrown from thread in background without an message loop cannot be handled by it.
    Wednesday, February 13, 2008 10:03 AM

All replies

  •  

    Hi kareno12,

     

    1. It doesn't seem to be good idea to throw exceptions from painting events. Probably, control which raises this event handles and swallows custom exceptions on painting events. I suggest you to have a look at control's code.

     

    2. Generally speaking, BackgroundWorker component swallows exception, writes it to internal error field and then passes it in RunWorkerCompletedEventArgs at the end of the execution.

    Tuesday, February 12, 2008 8:16 AM
  • Hi Vitaliy,

     

    1.  The control handling the event does not have any exception handling in it.  Also since the system exception is able to be thrown up the stack I would have expected the custom exception (based on System.Exception) to be able to be thrown as well.  

    Has any one heard of the microsoft events (ie OnPaintCell, OnUpdate, OnClose) catching exceptions and "handling" them?

    I would think that all of the micorsoft base classes would not catch any exceptions but let them pass back up the stack, but the way my code is acting is as if something is catching the exception in the OnCellPaint method even though I have nothing there.

     

    2.  The way I am handing the BackGround worker is in RunWorkerCompleted i have

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

    {

         if (e.Error != null)

         {

                throw (new MyNewException("some message", e.Error, mySeverity);

         }

    }

     

    where myNewException is (in a simplifed form)

    public partial class MyNewException: Exception

    {

         private string mySeverity;

         public MyNewException(string message, Exception ex, string severity):

              base(message, ex)

         {

              mySeverity = severity;

         }

    }

     

    Thanks Karen

    Tuesday, February 12, 2008 1:49 PM
  • I assume you're talking about .net 2.0 and above.

    AppDomain.UnhandledException is just a notifyer of an exception in the current AppDomain, it is not a exception handler like the catch block, it won't swallow the exception. The exception will cause the whole application down. You can get a chance to do some logging option in the event handler, but you cannot stop the application to be shut down by CLR.

    Application.ThreadException can only capture and hanlde the exception thrown in Windows Forms threads, though the exceptions thrown from thread in background without an message loop cannot be handled by it.
    Wednesday, February 13, 2008 10:03 AM