473,770 Members | 1,826 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.ApplicationNa me = "c:\\nothing.ex e";
t.Flags = TaskFlags.RunOn lyIfLoggedOn;
t.SetAccountInf ormation(UserNa meTextBox.Text, PasswordTextBox .Text);
t.Flags = TaskFlags.Syste mRequired;
t.Priority = System.Diagnost ics.ProcessPrio rityClass.High;
DateTime l_ExecTime = DateTime.Now.Ad dDays(1);
t.Triggers.Add( new RunOnceTrigger( l_ExecTime));
t.Save();
st.DeleteTask(" temp63846");
return true;
}
catch (UnauthorizedAc cessException 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.P ageTitle, " page's
ShowDialog()");
l_CurrentPage.S howDialog();
}
catch (System.Runtime .InteropService s.COMException e)
{
string l_Error = string.Format(" An exception was thrown during
execution of the page '{0}'. Exception: {1}", l_CurrentPage.P ageTitle,
e.Message);
Logger.LogError (l_Error);
System.Windows. Forms.MessageBo x.Show(l_Error, "Initializa tion error",
System.Windows. Forms.MessageBo xButtons.OK,
System.Windows. Forms.MessageBo xIcon.Error);
}
catch (Exception e)
{
string l_Error = string.Format(" An exception was thrown during
execution of the page '{0}'. Exception: {1}", l_CurrentPage.P ageTitle,
e.Message);
Logger.LogError (l_Error);
System.Windows. Forms.MessageBo x.Show(l_Error, "Initializa tion error",
System.Windows. Forms.MessageBo xButtons.OK,
System.Windows. Forms.MessageBo xIcon.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 1918

You might try adding handlers for

AppDomain.Curre ntDomain.Unhand ledException
Application.Thr eadException

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

hth,
Alan.

Feb 16 '06 #2
I tried implementing an AppDomain.Curre ntDomain.Unhand ledException
event handler with the following code and I am still getting unhandled
exceptions.

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

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

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

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

static void Main(string[] args)
{
AppDomain.Curre ntDomain.Unhand ledException += new
UnhandledExcept ionEventHandler (handler);
---------------------------------------------------------------------------*--------------------------------------------------------

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

static void handler(object sender, UnhandledExcept ionEventArgs 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? The following 2 first lines of code - I think - can't raise exceptions: Try Dim str As String Dim intVar As Integer
8
3554
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 County, Wisconsin USA NET csgallagher@REMOVETHISTEXTmetromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/
8
2737
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? (ie, forget the finally block and just end the try/catch and put the code after the try/catch block). Or does the "finally" construct add some additional functionality?
11
3492
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
3083
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 block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
9
1652
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! Especially at 2.8 GZ and 1GB RAM. The same pause happens on different situations: database access Trys, Array access Trys. Hummm??
32
6128
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 understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
23
2333
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
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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 we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.