Solved

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

Posted on 2012-04-11
10
325 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

636 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