Solved

C# question. Debugged complaint: 'Exception was unhandled by user code'

Posted on 2012-04-11
10
308 Views
Last Modified: 2012-06-27
Hi experts

I wonder if someone can tell me what the problem is, as I got the debugger complaint 'Exception was unhandled by user code'. My intention was if the condition fails, the method would throw an exception and would be handled by the caller (or caller of the caller). Had I done anything wrong here?

        private static SomeClass Method_1(BackgroundWorker backgroundWorker)
        {
            SomeClass response;
            int retryCounter = 0;

            do
            {
                response = Utility.DoSomething();
                if (response == null)
                {
                    // Increment 'retryCounter' and re-try for up to 3 times.
                    retryCounter++;
                    if ((retryCounter <= 3) && (!backgroundWorker.CancellationPending))
                    {
                        continue;
                    }

                    // Quit! Then return 'false' after re-trying 3 times.
                    string message3 = "No response to after 3 retry attempts.";
                    throw new Exception(message3);
                }

                return response; // ??? At here, VS2008 complaint 'Exception was unhandled by user code' ???

            } while (!backgroundWorker.CancellationPending);

            return response;
        }
0
Comment
Question by:dominicwong
  • 5
  • 2
  • 2
10 Comments
 
LVL 25

Expert Comment

by:Sinisa Vuk
Comment Utility
Because you generate custom exception with:

throw new Exception ....

... and there is no try..catch to handle it.
Is it function or method? In function you don't need to throw anything - but you can return error code.
0
 

Author Comment

by:dominicwong
Comment Utility
the try..catch is in the caller.
And, it is a method.
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
Comment Utility
Why you then return value if it is method? Methods should be void.
0
 

Author Comment

by:dominicwong
Comment Utility
I see what you meant. It is a function. The 'response' is an object returned to the caller.
0
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

 

Author Comment

by:dominicwong
Comment Utility
Hi experts, is this a warning from VS2008 or the way to do it is not correct? It is because exception handling should be abled to handle it outside the method.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
If your caller (or caller of caller, etc.) does not catch that particular type of exception (or an equivalent parent exception), then your exception will bubble up all the way to to the top of your call stack and then force the runtime to abort with the exception you are witnessing. Make sure you have a catch block which has a matching exception type (or parent type).

For example, you are throwing a new Exception. Are you catching that type? If your caller looked like this:

try
{
    SomeClass Method_1(bw_instance);
}
catch (InvalidOperationException)
{

}

Open in new window


...then you would not have an appropriate handler for your exception. The reason is that InvalidOperationException is a child class of Exception--not a parent. If instead you had SomeClass Method_1 throwing an InvalidOperationException:

private static SomeClass Method_1(BackgroundWorker backgroundWorker)
{
...
    throw new InvalidOperationException(message3);
...
}

Open in new window


..and then you handled Exception in your caller:

try
{
    SomeClass Method_1(bw_instance);
}
catch (Exception)
{

}

Open in new window


...then your code would not result in an unhandled exception because Exception is a parent of InvalidOperationException.
0
 

Author Comment

by:dominicwong
Comment Utility
Thanks kaufmed.
0
 

Author Closing Comment

by:dominicwong
Comment Utility
Thank you.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
NP. Glad to help  = )
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now