PDA

View Full Version : Application.Exit in a catch block


ColinHofmann
11-29-2003, 02:04 PM
Heya all,

This is a lame question, I'm sure... But I'm just getting started with C# so I am a bit naive...

In any case, I am trying to figure out why my application isn't exiting... Here's a block of code involved:


DSPortAdapter adapter = null;
try
{
adapter = OneWireAccessProvider.getDefaultAdapter();
}
catch (Exception ExceptionType)
{
MessageBox.Show(ExceptionType.ToString());
Application.Exit();
}

// Start exclusive access to the 1-Wire network
adapter.beginExclusive(true);


Now, the try block works right... The catch block sort of works right... It catches the exception when thrown, displays the MessageBox, but then does not exit!!

Instead it seems to blindly ignore my Application.Exit() statement and continue running at the adapter.beginExclusive(true) statement...

This is bad because if an exception was thrown above in the try block then the adapter object was not instantiated... hence I get yet another exception when I try to callt he beginExclusive method on the non-instantiated adapter object...

Why would C# ignore my request to exit the application?

I understand that the Application.Exit method flushes all the queues, but how do I make an application EXIT when I want it to?

Any suggestions would be appreciated...

Thanks,
Colin