Solved

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

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

16 Experts available now in Live!

Get 1:1 Help Now