C
CodeSlayer
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
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