471,570 Members | 892 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

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

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

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

Similar topics

6 posts views Thread by Erik Cruz | last post: by
8 posts views Thread by clintonG | last post: by
11 posts views Thread by Pohihihi | last post: by
23 posts views Thread by VB Programmer | last post: by
9 posts views Thread by Bob Achgill | last post: by
32 posts views Thread by cj | last post: by
23 posts views Thread by pigeonrandle | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.