Exception Handling DO NOT WORK!! Help Please!@!

Hi.

I want that you think with me...
When i put a try-catch exception handler block in an application
what i expect from it

i expect that it will handle the error and begin the normal flow of the application.

Now, you aswer me, why it doesn’t work in this scenary :

I have one Form1 (Project > Add New > Form, nothing different), and another Form2 and one Form3. i’m proud of my creativity for names.

Form1 :

private void Form1_Load( object sender, EventArgs e )
{
try
{
new Form2().Show();
}
catch (Exception ex)
{
MessageBox.Show("Error calling form 2" + ex.Message + ex.StackTrace);
}
}

Form2:

private void Form2_Load( object sender, EventArgs e )
{
try
{
new Form3().ShowDialog();
}
catch (Exception ex)
{
throw new Exception("Form2 loading.. calling form3..",ex);
}
}

Form3:

private void Form3_Load( object sender, EventArgs e )
{
throw new Exception("Form3 loaded");
}

Cool, this is very simple...
I just wanna throw an exception on form3, the rethrow it on form2,
and then catch and ignore it on form1.. and let the normal flow continue...

But what happen is : After handle my exception on form1, the debug continue stepping out throug the application.run() call and then, exits.

Estrange things : If i put the same code on the form1, inside a button_click event they don’t stop the application, and the form still running...

WHY

How i said, i EXPECT THAT THE TRY-CATCH BLOCK HANDLE AN EXCEPTION. just this.

Can you help me

Thank you all.



Answer this question

Exception Handling DO NOT WORK!! Help Please!@!

  • Siddharth Bhatia MSFT

    I will try again...

    On the mobile device, is worst then on PC.

    On Mobile i can’t rethrow, can’t repack... nothing...
    if an Exception is generated in one of my "sub-forms" the application just stop.

    it seems to be a bug, i opened a bug report.
    and i really don’t wanna catch e handle exceptions on ALL methods to prevent a unhandled exception.

    if i can rethrow a new exception and then handle it on a unique place, then would be a good idea.. but i can’t.

    And i think that doesn’t matter, i need to have a way to handle a load exception too, a load is a procedure like the other procs. It is just called by an event, nothing asyncronous, nothing in other thread.

    I design software for .net for 4 years, and it seems to be a bug for me.

    Try it on framework 1.0, 1.1.. before do a new post.


  • will rpberts

    Wagner Bertolini Junior wrote:
    On Mobile i can’t rethrow, can’t repack... nothing...

    Yes you can, i wrote a little test and i can bubble up or rethrow exceptions.

    Wagner Bertolini Junior wrote:
    if an Exception is generated in one of my "sub-forms" the application just stop.

    This is because your exception is not handled. This is normally, when a method/event is executed on a other thread you can only catch it if you are on the same thread and you are the caller.

    Wagner Bertolini Junior wrote:
    a load is a procedure like the other procs. It is just called by an event, nothing asyncronous, nothing in other thread.


    When this event was invoked by a Control or Component most of the time it will be executed on a other thread.

    Wagner Bertolini Junior wrote:
    I design software for .net for 4 years, and it seems to be a bug for me.

    Try it on framework 1.0, 1.1.. before do a new post.

    This is not a bug, this i normal. This is in every environment, Winforms, Webforms, Compact Framework, Java... etc.

    In the compact framework for Pocket PC there is no event that is called on a unhandled exception. So you must, like in every design hande exceptions inmidiatly and don't lett them bubble out of the event. Just created your own static event and fire this whenever you catch a exception that you can't handle.

    Again, when you create your own static event i don't see any problems anymore.



  • Tim Smith

    I don’t understand why is another thread

    If the Load procedure was processed by another thread, would not be possible to access local variables of the form directly, we would use the Invoke to execute them.

    But ok, let’s talk about another sample that shows better my use.
    I’m just going crazy..

    I build an application.. 3 forms again,

    the first call the second that call’s the third...

    on the third put a button and raise an exception..
    catch the exception and rethrow it...

    (and talking about rethrow what’s the difference of not catch and rethrow if i don’t change the exception )

    continue...
    when the third form "rethrowed" the exception the framework raises the exception, i expect that the exception would by throwed up to the parent form...

    i think that im with serious problems to understand this..
    can you help me


  • Steven Weinert

    The load event is invoked from a other thread. So you can't catch it with your way.

    A load event should never throw an exception! Because it is outside the UIThread.



  • Jim Hollcraft

    I’m using .Net Framework 2.0

    with a Device Application project for the Windows CE 5.0 target.


  • DennisR

    You can rethrow.. but not between forms.

    I don’t see how a static event can solve my problem.

    i need something to assure that the main program will not close.. with this static event i can’t prevent unhandled exceptions, then i will need to put a try-catch everywhere.

    and this don’t happen in Java... i’m a java programmer too.

    i will wait for a solution from MS.


  • Pat2214

    The Load event is raised on the UI thread. It has to be because all interaction with the UI must occur on the thread that created the UI. If Load was raised on a separate thread then you could not initialize controls or do anything else in Load.

    I agree with PJ however that the design is bad. Hopefully you were just testing a theory. Firstly you are creating a form that creates a separate modeless form. When you call Show on Form2 it might or might not be displayed prior to control returning to Form1.Load and therefore it might or might not be in an EH block. The reason is that Load may very well cause a message to be posted to the queue. Depending on how that message is generated it might either invoke the message handler (and therefore Load eventually) directly or it might be posted and therefore will be handled sometime after the current message (Form1.Load's invoker) returns. Therefore an EH around a Show call may or may not catch exceptions from the other form. The ShowDialog call will work as expected because control won't return to Form2.Load until Form3 is dismissed.

    However that doesn't really resolve your problem because if you move the creation of the form to, say, a button handler that is invoked when you click a button the main form will still go away. It appears that the underlying framework is getting confused about the exception occurring while loading the form and for some reason thinking it is the main form. It therefore terminates the main form. The biggest issue here is that nobody is notified. I dropped a custom ApplicationContext in and it never got notification that the main form was closing or anything. Even the main form's FormClosing method isn't invoked. It would definitely appear to be a bug in the implementation. This can cause resource and memory leaks if the form allocated any because nothing is called when the form goes away. I can understand your specific case failing but in general calling Show on a form that will throw shouldn't bring down the app. You should submit a bug report to MS with the sample code that you gave. FYI I used C# so it isn't an issue with how VB works.

    Michael Taylor - 1/25/06


  • Angelo_B

    I think your design is bad, why should you throw exception's in events A exception in a SelectedIndex changed of a Combobox, where do you expect it can be handled


  • Osama Ibrahim

    I’m just exposing something that isn’t my actual design.

    The only use of thaat thing for me, is to catch unexpected exceptions that may occur.

    I have an ERP on a mobile device, e this ERP has a lot of funcionalities, but if 1 FAIL i just don’t want that the ERP closes. JUST THAT.

    I don’t think i’m with a wrong design since i’m using all that i see about exceptions like specialized expections and more. The problem is that the Application is unstable. When someone tells to us that we can use a try-catch block to prevent an error, and it doesn’t work as expected, isn’t a good thing.

    The references (MSDN) don’t tell nothing about this, and this threads.. just this.

    thank u.


  • sadavies

    Where do you throw these thing In events again, if so there are a lot of events that are executed on a other thread this can give the problem.

    When you are using .NET Framework 2002/2003 you can use Application.ThreadException event to get all Exception that aren't catch/handeled.


  • Jens Karlsson

    Wagner Bertolini Junior wrote:
    I have an ERP on a mobile device, e this ERP has a lot of funcionalities, but if 1 FAIL i just don’t want that the ERP closes. JUST THAT.

    You must handle the exception where it can hapen. I think you trying to handle the exception on incorrect places.

    Where you can expect an exception you must handle it, or you must know for sure you can safelly repack/rethrow the exception. This is never in event!

    You can create your own ProcessException event. Then you not lett the exception bubble away, but you fire your own event. This is a common thing in multi-threading.



  • Milzit

    Hi,

    cannot be like that...
    most of the procedures are started by an user event... how to use try-catch in this conditions cannot be another thread... isn’t asynchronous.

    The examples that i give, talk about windows application, but i’m doing a Mobile Application where the problems are the same.

    But in a Mobile Application i don’t have the "Application.ThreadException"... the compact framework it’s very very "COMPACT".. i’m in pain with this.

    Thank u.


  • RenRen

    I think that all the 3 forms are in the same thread, if you say that excuse-me, i didn’t understand.

    But i got your point, and the design question i answer to PJ, please look at it.

    I wanna know where can i report a bug, the last time that i do that i ask someone to do it for me.. can u show me a link to this section

    Thank u


  • Yann

    Application.ThreadException is available for the Compact Framework 1.1, wish version do you use


  • Exception Handling DO NOT WORK!! Help Please!@!