Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

The following is for a windows application. If I was to cause a button click to throw an exception i.e.

 

private void button6_Click(object sender, System.EventArgs e)

{

//simulate a crash by throwing an exception

throw new Exception ("Simulated Crash");

}

 

 

where would I catch this other than in this function? I tried catching it in the Form's main fuction :

 

static void Main()

{

try

{

Application.Run(new Form1());

}

catch (Exception ex)

{

Console.WriteLine (ex.ToString());

}

}

 

but it seems like I can't catch it there.

Posted
where would I catch this other than in this function?

In any function which occurs before it on the stack, so anything that sits between the Main method and the event handler method.

 

I tried catching it in the Form's main fuction :

...

but it seems like I can't catch it there.

That works, why do you think it doesn't.

Posted
In any function which occurs before it on the stack, so anything that sits between the Main method and the event handler method.

 

 

That works, why do you think it doesn't.

 

The program is still crashing and I'm not catching the exception at all.

  • Leaders
Posted

Take a look at these two events:

'Unhandled exception in AppDomain (I don't think this will catch an exception before it crashes the GUI)
AppDomain.CurrentDomain.UnhandledException
'Unhandled exception in GUI thread
Application.ThreadException

There is usually nowhere between Application.Run and the button click event handler to attatch an exception handler. There will be nothing on the stack between the two besides non-user code, making it hard to catch an exception and keep your app from crashing.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...