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

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

Post by CodeSlaye » Fri, 17 Feb 2006 08:16:07


i 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.M
 
 
 

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

Post by Alan » Fri, 17 Feb 2006 09:54:38


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.

 
 
 

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

Post by CodeSlaye » Sat, 18 Feb 2006 03:42:25

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