473,511 Members | 16,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1894

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
552
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
8
3529
by: clintonG | last post by:
Could someone provide me with a URL documenting the specific order of exceptions? -- <%= Clinton Gallagher A/E/C Consulting, Web Design, e-Commerce Software Development Wauwatosa, Milwaukee...
8
2721
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
11
3460
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
23
3040
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
9
1636
by: Bob Achgill | last post by:
I really like this function but have tried to slow down on using it because I get a 1 second pause each time I use it. I don't really understand why the computer has to think for 1 second! ...
32
6085
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
23
2293
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.