By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

Catch block is failing to catch exceptions when not run from MSDev

P: n/a
Hi all,

This one really has me and the other .Net developers at my work
stumped. I have an application that is doing the following:

1 - attempt to validate that user can create a windows task via COM
interops
2 - an exception is thrown because user doesn't exist
3 - Exception is caught by calling code shown below:

-----------------------------------------------------------------------------------------------------------------------------------
START CODE
-----------------------------------------------------------------------------------------------------------------------------------
try
{
Task t;
ScheduledTasks st = new ScheduledTasks();
t = st.CreateTask("temp63846");
t.ApplicationName = "c:\\nothing.exe";
t.Flags = TaskFlags.RunOnlyIfLoggedOn;
t.SetAccountInformation(UserNameTextBox.Text, PasswordTextBox.Text);
t.Flags = TaskFlags.SystemRequired;
t.Priority = System.Diagnostics.ProcessPriorityClass.High;
DateTime l_ExecTime = DateTime.Now.AddDays(1);
t.Triggers.Add(new RunOnceTrigger(l_ExecTime));
t.Save();
st.DeleteTask("temp63846");
return true;
}
catch (UnauthorizedAccessException e)
{
// Credentials aren't valid
// TODO: Add code to delete task if it was created
string l_Error = string.Format("The password is incorrect or the
account you specified is either not a valid account or does not have
permissions to run the Windows Task Manager task. Please specify an
account with permissions to run the Windows task. Error: {0}",
e.Message);
Logger.LogError(l_Error);
return false;
}
catch (COMException e)
{
string l_Error = string.Format("Error: An exception was thrown while
trying to validate the user. Exception: '{0}'", e.Message);
Logger.LogError(l_Error);
throw;
}
catch (Exception e)
{
string l_Error = string.Format("Error: An exception was thrown while
trying to validate the user. Exception: '{0}'", e.Message);
Logger.LogError(l_Error);
throw;
}
-----------------------------------------------------------------------------------------------------------------------------------
END CODE
-----------------------------------------------------------------------------------------------------------------------------------

4 - Exception is thrown further up the stack to try\catch block that
writes out error to user. Code is shown below:

-----------------------------------------------------------------------------------------------------------------------------------
START CODE
-----------------------------------------------------------------------------------------------------------------------------------
try
{
Logger.LogHigh("Calling ", l_CurrentPage.PageTitle, " page's
ShowDialog()");
l_CurrentPage.ShowDialog();
}
catch (System.Runtime.InteropServices.COMException e)
{
string l_Error = string.Format("An exception was thrown during
execution of the page '{0}'. Exception: {1}", l_CurrentPage.PageTitle,
e.Message);
Logger.LogError(l_Error);
System.Windows.Forms.MessageBox.Show(l_Error, "Initialization error",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
catch (Exception e)
{
string l_Error = string.Format("An exception was thrown during
execution of the page '{0}'. Exception: {1}", l_CurrentPage.PageTitle,
e.Message);
Logger.LogError(l_Error);
System.Windows.Forms.MessageBox.Show(l_Error, "Initialization error",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
-----------------------------------------------------------------------------------------------------------------------------------
END CODE
-----------------------------------------------------------------------------------------------------------------------------------

If you run the code from the debugger in either Debug or Release
configuration everything works as expected and the user gets an error
box letting them know that the user could not be found. If you run the
code by double clicking on the executable or launching it from another
process then the exception isn't caught the second time and you get an
unhandled exception dialog complete with a details button for stack
trace. As you all know, it's not a very nice way to present an error to
an end user so this needs to be changed. I have tried all day to figure
out what the problem here is and can't get the exception to be caught
on in the second block.

Any ideas on how to make this work would be greatly appreciated,

Ryan

Feb 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

You might try adding handlers for

AppDomain.CurrentDomain.UnhandledException
Application.ThreadException

don't guarantee that it will catch them but its somewhere to start.

hth,
Alan.

Feb 16 '06 #2

P: n/a
I tried implementing an AppDomain.CurrentDomain.UnhandledException
event handler with the following code and I am still getting unhandled
exceptions.

---------------------------------------------------------------------------*--------------------------------------------------------

START CODE
---------------------------------------------------------------------------*--------------------------------------------------------

---------------------------------------------------------------------------*--------------------------------------------------------

END CODE
---------------------------------------------------------------------------*--------------------------------------------------------

static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(handler);
---------------------------------------------------------------------------*--------------------------------------------------------

START CODE
---------------------------------------------------------------------------*--------------------------------------------------------

static void handler(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show("Thankyou Jesus!");
}
---------------------------------------------------------------------------*--------------------------------------------------------

END CODE
---------------------------------------------------------------------------*--------------------------------------------------------

Feb 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.