Solved

Unhandled Exception handler provided by VS-2005 is preempting my Exception Catch Clauses

Posted on 2008-09-30
32
1,191 Views
Last Modified: 2013-11-07
EE Experts:

One day not so long ago I was trying to make changes to my solution configuration parameters selected in VS2005 IDE and something caught my eye with reguard to the processing of unhandled exceptions. I already have an exception handler of last resort that catches all exceptions that occur in my application that I don't catch myself with a Try/Catch block provided to me by Idle_Mind. It has been serving me very well for more that a year, but when I saw this, my curiosity got the best of me and I thought I would try it. It has been a month or so since I made that mistake because now I can't find the check box that I checked to get it and I want to make it go away because it preempts all my Try/Catch block exception handlers. I don't get to see the messages that those Try/Catch blocks provide and I miss not having them.

Here is how I test by forcing a "General Exception" to Occur. In my application I have a method that interfaces to Microsoft Word 2007. When my application starts up it creates an instance of MS-Word and assigns it to a global object reference variable. If the user closes that instance of MS-Word by accident, which I have trained them not to do, my program will throw an exception because the object reference variable no longer points to an instance of an object. So I can force an exception by closing the instance of MS-Word and then execute a function that trys to create a document by using that instance of MS-Word that isn't there anymore. Please see attached code snippet.

The "Catch ex As Exception" never gets executed. Instead I get a dialog box provided by the .NET Framework, I assume that is where it is coming from,  that has a title bar that exactly matches character for character the title bar of my application at the time the exception occurs. The content of that dialog box is: "Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately."

"The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)."

In this context I assume that RPC stands for Remote Procedure Call. Which is the call my program was making to MS-Word. Unless I am wrong about this, I believe the solution to my problem is going to be a simple uncheck a check box some place in VS2005 IDE, but where? That is my question.

Thank you,
Ted Palmer
Public Sub PrintMMDoc_MS_Word(ByVal strMMDataDir As String, ByVal strMMDataFile As String, ByVal strMMDocDir As String, ByVal strMMDDocFile As String)

        '2007-06-03 Ted Palmer

        'Dim wrdApp As Word.Application: Instantiated in class frmLogin >> cmdLogin_Click event.

        'Closed & Disposed in class MainMDIFrame >> Closing event
 

        go_wrdApp.Visible = True
 

        '2007-05-24 Ted Palmer

        'This Try-Catch block uses MailMerge functionality of "Word.Application" to print an instance

        'of the MS-word document passed as a parameter to this subroutine and 

        'the MailMerge data file passed as a parameter to this subroutine.

        Try

            'Open an existing MailMerge Main document.

            Dim wrdDoc As Object

            go_wrdApp.ChangeFileOpenDirectory(strMMDocDir)

            wrdDoc = go_wrdApp.Documents.Open(FileName:=strMMDDocFile) '<-- MS-Word MailMergeMain document
 

            wrdDoc.MailMerge.MainDocumentType = 0

            wrdDoc.MailMerge.OpenDataSource(Name:= _

                strMMDataDir & strMMDataFile, ConfirmConversions:=False, _

                 ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _

                PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _

                WritePasswordTemplate:="", Revert:=False, Format:=0, _

                Connection:="", SQLStatement:="", SQLStatement1:="", SubType:=0)

            With wrdDoc.MailMerge

                .Destination = 0

                .SuppressBlankLines = True

                With .DataSource

                    .FirstRecord = 1

                    .LastRecord = -16

                End With

                .Execute(Pause:=False)

            End With
 

        Catch ex As Exception

            MessageBox.Show("In Module MainModule >> Sub PrintMMDoc, there was an Error in the MailMerge process. " _

            & "YOU the user probably caused this exception by closing the Microsoft Word application frame. " _

            & "Please don't do this again. You probably did this during the Print Preview process. During that process. " _

            & "you must close the MS-Word document and the MS-Word MailMerge Main document, but not the application frame. " _

            & "When closing the MailMerge Main document, you will see 2 small x(s) in the upper right corner of the application " _

            & "frame. Click ONLY the bottom x. Then minimize the application frame. To recover from this condition " _

            & "and restore the printing subsystem, you must exit the AutoSubrogate(TM) program and restart it again. " _

            & "Please save your work and restart the AutoSubrogate(TM) program now: " & ex.Message, _

            "AutoSubrogate(TM) Exception:", MessageBoxButtons.OK, MessageBoxIcon.Stop)

        End Try
 

    End Sub 'PrintMMDoc_MS_Word

Open in new window

0
Comment
Question by:Ted Palmer
  • 19
  • 7
  • 5
32 Comments
 
LVL 13

Expert Comment

by:Michael_D
ID: 22611723
I think you are talking about this: MainMenu->Debug->Exceptions. You can click on Reset All button - this will set User-unhandled boxex.
0
 

Author Comment

by:Ted Palmer
ID: 22612664
Michael_D:

Thank you. That looked to me like it was going to do it for me, but it didn't work. That was the first time I had actually seen that menu choice. I haven't been to that screen before till I followed your instructions. I tried expanding the tree nodes to see if I recognized anything -- I did not. After I clicked "Reset All", I saw no changes in any of the check boxes. I do remember that this is not where I changed any option that I selected to start getting this new to me behavior for user exception handling.

I probably should have looked around the IDE more to see if I could find something. When I selected the option that caused me to get user exception handling service provided by Visual Studio or .NET, I started by right clicking a node in the Solution Explorer tree like the Solution node itself or my main Project node. My Solution only has 3 Projects: my main Project with all of my Windows screen forms and a module I call MainModule; a support Project that compiles into a DLL that has the 2 Windows screen forms that I inherited most of the Windows forms from in my main Project; and a Setup Project that creates my deployment package. I am confident that I right clicked my main Project node or a node under it like "My Project", which was provided for me by the IDE, then clicked Properties.

I don't think it was "app.config", because when I right click it and select Properties, the Properties tab of Solution Explorer gets the focus and I see nothing there that looked like anything I used to get the user exception handling service that I now don't want and I am trying to get rid of.

Thank you for your help. Please tell me in what country are you located?

Ted Palmer
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 22613621
Ted,

You might find this article helpful: http://blogs.msdn.com/scottno/archive/2005/05/10/416133.aspx
It explain how to turn "the new feature" of VS2005 IDE on. So I believe the reversal action might solve your problem.

HTH

P.S. I am from Canada
0
 

Author Comment

by:Ted Palmer
ID: 22614592
Michael_D:

Thanks again, but this didn't help. The date on the article is 2005-May-10. It would seem that VS2005 has changed since then because the menu path the author gives to follow in most places do not exist in my version of VS2005. I have version 8.0.50727.762 (SP.050727-7600). Microsoft .NET Framework Version 2.0.50727 SP1.

I was able to go to MainMenu >> Debug >> Exceptions >> Common Language RunTime Exceptions and uncheck User-Unhandled. I recompiled and tested as described above, but I got the same result that I did not want. I also tried MainMenu >> Tools >> Import and Export Settings which gave me a Wizard with 3 radio buttons. One of which was labeled "Reset All Settings". After running this Wizard, which took a suprisingly long time, I still got No Joy.

Finding a fix for this is not your obligation, but if you have any more ideas, I would be thrilled to know about them.

Thank you,
Ted Palmer
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 22615291
Is your Tools->Options->Debugging->General->Enable Just My Code option checked?
0
 

Author Comment

by:Ted Palmer
ID: 22616322
I have Tools->Options->

After that my menu does not have <<Debugging>> on it.

Thank you for your help,
Ted Palmer
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 22616744
<<Debugging>> is not menu entry but it is branch on the treeview in Options Dialog
See attached screenshot with "Enable Just My Code" highlighted
JustMyCode.PNG
0
 

Author Comment

by:Ted Palmer
ID: 22617245
Michael_D:

This is what I have on my Options Tree: (I may have to look for an update or Service Pack to get the same Tree under Options that you have.)
MyToolsMenu.PNG
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 22617297
Try to select "Show all Settings" checkbox in bottom-left corner before aplying any SP:)
0
 

Author Comment

by:Ted Palmer
ID: 22618182
Michael_D:

My Options dialog box now looks just like your's except for one CheckBox which I don't think that one effects what I am having a problem with. That seemed pretty slow on my part not to get that "Show all Settings". Thanks for your patience. Now that I have that issue taken care of, I recompiled my executable and I am still getting the same thing. I didn't know that this was going to be so hard. I hope there is some other way out of this.

Ted Palmer
MyToolsMenu1.PNG
0
 

Author Comment

by:Ted Palmer
ID: 22669122
I posted a request for assistance in Community Support. I am waiting for a response.

TedPalmer
0
 

Author Comment

by:Ted Palmer
ID: 22750106
Please do not delete my question. I still need an answer. I will post another  request for assistance to the Community Support Zone as instructed in the E-mail alert that I received that I believe is the start of the "abandoned request of assistance" process. This will be the 2nd one. I'll try to improve on my request this time. Maybe make it shorter. In my last request I did offer a suggestion. There is not a specific menu choice "Request for Assistance" in the Community Support Zone so I am not sure that my request is even being seen because I am not sure that I am posting in correctly.

Ted Palmer
0
 
LVL 18

Expert Comment

by:mdougan
ID: 22776934
Not sure where the checkbox is that you checked, however, by checking it, it may have added some things to your code.  Search your entire project sourcecode for the following keywords and let me know if you find any instances of these.

AppDomain
UnhandledException
ThreadException
0
 
LVL 18

Expert Comment

by:mdougan
ID: 22777226
What is the Startup Procedure for your application?  Is it your main form, or do you have a Sub Main?  If a Sub Main(), make sure that the module it is in imports the library System.Windows.Forms at the top, then add this line to the beginning of the sub.

Sub Main()
   Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)
......
End Sub

If your Project Startup is your main form, you can put that line at the top of your form load event.

It is possible that you set your application's UnhandledExceptionMode through the property window, but not sure what you would need to highlight to see properties for the application object.

As long as we're trying things, while your project is open, try pressing

Crtl + Alt + E  

See if this is the dialog with the checkboxes that you remember.
0
 

Author Comment

by:Ted Palmer
ID: 22777317
mdougan:

Thank you for helping me. I am researching your original question about the occurances of the 3 strings. This is the first one that I found, but it is "decoy code" I left in to frustrate anybody that might try to reverse engineer my software. It is the way I started my application from a tutorial I read, but changed to a "StartUpForm" named MainMDIFrame. See snippet for what I found when searching for AppDomain. This is all I have found so far.

Do you want me to continue searching for the other 2 strings?

This was found in MainModule.vb -- It is "decoy" code to frustrate anybody reverse engering my code.
 

    'Run the application

    'The main entry point for the application. As of 2007-05-31 Still True. Ted Palmer
 

    <STAThread()> Public Sub Main()

        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledException

        AddHandler Application.ThreadException, AddressOf ThreadError
 

        System.Windows.Forms.Application.Run(New MainMDIFrame)

        'System.Windows.Forms.Application.Run(New MainForm) <-- This is real old. For reference only. Do NOT turn this loose. 2007-03-31 Ted Palmer

    End Sub
 

++++++++++++

Above is only occurance I found of "AppDomain"

++++++++++++

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Ted Palmer
ID: 22777435
mdougan:

This snippet is all I found for "UnhandledException". It is just above the code in the last snippet. I am sure that it is dead code. abandoned in place to frustrate those who would reverse engineer my software. BTW: I have set Search Hiden Text checked in my string search dialog ctl-F.

TedPalmer
    'As of 2007-05-26 Ted Palmer

    'This Sub is a critical component to this application

    Private Sub UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)

        MessageBox.Show("There was an unhandled exception", "AutoSubrogate(TM) Exception:", MessageBoxButtons.OK, MessageBoxIcon.Stop)

    End Sub

    'As of 2007-05-26 Ted Palmer

    'This Sub is a critical component to this application

    Private Sub ThreadError(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)

        MessageBox.Show("There was an unhandled thread error. Object: " & sender.GetType.ToString, "AutoSubrogate(TM) Thread Error:", MessageBoxButtons.OK, MessageBoxIcon.Stop)

        'MessageBox.Show("There was an unhandled thread error. Object: " & sender.ToString, "AutoSubrogate(TM) Thread Error:", MessageBoxButtons.OK, MessageBoxIcon.Stop)

    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:mdougan
ID: 22777473
Well, the two lines 7 and 8 are exactly what I was looking for.  Those two lines are what you would add to cause your application to automatically handle unhandled exceptions.

If I understand your comment about decoy code, you are suggesting that this code does not actually execute?

Just to be safe, you should comment out both of those lines and see if it has any effect.  If not, then try my second comment above.
0
 

Author Comment

by:Ted Palmer
ID: 22777614
mdougan:

My search for "ThreadException" yielded all of the above in the 2 code snippets and that is all. Maybe it is not dead code. Idle_Mind gave me some code to use as an Exception Handler of "Last Resort" and it has been working for me without any problems so far and doing a good job. Please see the attache screen shot in a file for what starts my application. I will try your other suggestions now.

Thanks for your help.
TedPalmer
PropertiesOfStartUp.PNG
0
 

Author Comment

by:Ted Palmer
ID: 22777668
mdougan:

our messages collided. I am now suspicious of CheckBox "Enable application framework", but I bellieve I unchecked it in one of my previous tests. I will comment out the code you suggested; recompile and test.

TNX
TedPalmer
0
 

Author Comment

by:Ted Palmer
ID: 22777781
mdougan:

I just noticed line 10 of code in first snippet
System.Windows.Forms.Application.Run(New MainMDIFrame)

MainMDIFrame is my startup frame. I am pretty sure that this is the code given to me by Idle_Mind, but I will do as you asked about comment out code and test. I'll post the results as soon as I have them.

TNX
TedPalmer
0
 
LVL 18

Expert Comment

by:mdougan
ID: 22777805
If you want your form to be the actual startup procedure, then you need to leave Enable Application Framework checked.  Otherwise, it will force you to specify a proceedure such as Sub Main().

Now, you should click on the button at the bottom that says View Application Events.  One of the possible application events is the UnhandledException event.  There may be a way to turn that off through that dialog.
0
 

Author Comment

by:Ted Palmer
ID: 22777907
mdougan:

I commented out line # 7 & 8 as you requested. I forced an exception using a bug in my program that I discovered after I started having the problem with my exception handlers being high jacked -- preempted by the checkbox I checked. I do not have code to handle this kind of exception in by my application source code, but it is the kind of thing that used to be handled by the code that you had me commented out that I got from Idle_Mind and was not preempt my Try/Catch block exception handlers. Please see the attached screen shot.

TNX
TedPalmer
DialogIdoNotwant.PNG
0
 

Author Comment

by:Ted Palmer
ID: 22777981
mdougan:

This is what I got when I tried to View Application Events. It is a blank screen without any source code.

I'm still trying.

TNX
TedPalmer
ViewApplicaionEvents.PNG
0
 
LVL 18

Accepted Solution

by:
mdougan earned 500 total points
ID: 22778289
OK, so, the fact that you commented out those AddHandler lines of code causes your error handling to change, correct?  This is the type of error handling that you had before you made any changes suggested by Idle_Mind, and before you checked that mystery checkbox, correct?

So, we can be pretty sure that this code is being executed somehow in your application.  Normally, the way it would be executed is if you had Sub Main() selected in your Application Property dialog instead of the name of your MDI form, but you don't.  It is possible that somewhere else you are calling Sub Main() directly.

If Idle_Mind is the one who suggested adding those AddHandlers, and if the error handlers that they are pointing to, the two subs you posted above, UnhandledException and ThreadError are the ones that you've been happy with, then, you can go back and uncomment them.  And, what you could try is going back to your application properties and unchecking the Enable Application Framework box and select Sub Main() as the startup.

As far as the View Application Events dialog, you have to select something from the General listbox... not sure if it will be the name of your  mainMDIFrame or something else, but you could look for that.  What you might find is that there is another UnhandledException procedure there that is different than the one Idle_Mind gave you, and it may be that one that is actually handling your exceptions now.  By unchecking the Enable Application Framework box, you will be disabling all of these Application Exceptions.
0
 

Author Comment

by:Ted Palmer
ID: 22778368
mdougan:

I uncommented line #s 7 & 8 and unchecked CheckBox "Enable application framework" recompiled and I am still getting preempted and high-jacked by something. It now seems to me that even though in my screen shot of AutoSubrogateApp window it says that my startup form is MainMDIFrame with CheckBox "Enable application framework" unchecked and line of code

System.Windows.Forms.Application.Run(New MainMDIFrame)

inside of <STAThread()> Public Sub Main() line #6 in first code snippet that my startup is really Sub Main(). That is one of the choices in "Startup Form" ComboBox. Please see attached file. I am still getting what I don't want but I have figured out the fix thanks to you. I will post it in the next message.
DialogIdoNotwantWithCallStackTra.PNG
0
 

Author Comment

by:Ted Palmer
ID: 22778528
mdougan:

You have it..!! You described what I already tried before I saw your post in the 2nd paragraph of your last message posted. I unchecked CheckBox "Enable application framework" and made my Startup Form be "Sub Main" and that fixed my problem. My exception handlers are not being high jacked any more. Actually I haven't tested for that yet. I have to go back and do the test where I kill my instance of MS-Word to be sure that my Try/Catch block exceptions are getting through. I'll do that now. You actually gave me the solution, but I tried it, thanks to you, before I read it from you. You get the points. See screen shot for what I have been trying to see for almost a month now. More discussion to follow.

TNX
TedPalmer
DialogIdoWant.PNG
0
 
LVL 18

Expert Comment

by:mdougan
ID: 22778591
If you are curious about exactly what is hijacking your exception handling, you could try putting it back to the way it was, setting a breakpoint on the line that causes the exception  (by the way, you don't have to do anything complex with Word to generate an exception, just declare a variable x as int32 then say x = 1/0 and that will cause an exception).

When  the program stops on the instruction that will generate the exception, choose the debugger key to Step Into (F10?) , if the error handler that is hijacking your application is somewhere in your application code, the debugger should step into it and that might show you where it is.
0
 

Author Comment

by:Ted Palmer
ID: 22778944
mdougan:

I tried my kill MS-Word test and got the dialog box attached above instead of my Try/Catch block exception handling. But I think that is OK. At least I am now getting the exception handling that was given to me by Idle_Mind and I think there is another reason I am not getting my Try/Catch block exception handler. That can be another question.

My mistake must not have been a CheckBox like I thought it was. I must have just forgotten the directions that Idle_Mind gave me when he gave me the code for an "exception handler of last resort" that I have been using and happy with for more than a year now. BTW: I did e-mail Idle_Mind for help and I am not faulting him for not being able to help me. He has the Express version of Visual Studio 2005 so he was not seeing the options in the screen shot that I included in my exchange with Michael_D.

I believe my original mistake was that I changed my "Startup Form" from Sub Main() to MainMDIFrame which disabled the "exception handler of last resort" that Idle_Mind gave me. I let way too much time lapse between when I made the changes that were the cause of my belief that my exception handlers were being high-jacked and I actually tried to fix them. I wasn't having any problem after I made my changes so I just let them ride.

This makes a great case in point for "Test Driven Development" using NUnit test. If I had used "Test Driven Development" and ran my test scripts every night after a day of making changes, my automated testing should have discovered that I broke something the day I made the mistake.

I am now taking a class in beginner Java (for the 2nd time) using "Test Driven Development" and JUnit test. My understanding is that NUnit was cloned from JUnit. I am having to get familiar with Eclipse IDE to do this which is a real pain for me since my short term memory borders nonexistance. But I digress too far.

I'll stop writing now and close this question. If I could multiply the points for this question by a million I would. The best developers are not always the guys that rack up the points using comment and run techniques. I could write another essay about that.

THANK YOU so much..!! You really did a GREAT job on this one and I know it was time comsuming and not easy.

TNX..!!
TedPalmer
0
 

Author Closing Comment

by:Ted Palmer
ID: 31501840
Your solution was actually in the third paragraph of this comment. But I have written way more than you probably want to read already. You do GOOD work..!! That is probably the best complement that I ever got. THANK YOU..!!
0
 

Author Comment

by:Ted Palmer
ID: 22779106
The fix is really in the 3rd paragraph of the comment selected as the solution.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 22779361
Thanks, glad I was able to at least point you in the right direction.

I've started playing around with the NetBeans IDE... lots of stuff to assemilate.  Pretty powerfull though, allows you to work with many different languages besides Java... PHP, Ruby on Rails.  Has support for MySQL, plus the embedded JDB type applications.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now