Solved

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

Posted on 2012-04-11
10
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
10 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 37836259
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
ID: 37836774
the try..catch is in the caller.
And, it is a method.
0
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 37836788
Why you then return value if it is method? Methods should be void.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:dominicwong
ID: 37837140
I see what you meant. It is a function. The 'response' is an object returned to the caller.
0
 

Author Comment

by:dominicwong
ID: 37900482
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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37906711
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
ID: 37917869
Thanks kaufmed.
0
 

Author Closing Comment

by:dominicwong
ID: 37917871
Thank you.
0
 
LVL 75

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

751 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