Solved

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

Posted on 2012-04-11
10
313 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 26

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 26

Expert Comment

by:Sinisa Vuk
ID: 37836788
Why you then return value if it is method? Methods should be void.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# HTTP GET method sample code 3 57
Eagerly loading related objects in entity framework 5 29
use of unassigned local variable 'id' 6 38
SqlDataBase 7 48
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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