0

I have a try catch statement and when the code goes to throw the exception, it will not run past this point.

I press F5/F10 and the code keeps running the same line of code.

Is this to do with a setting in VS2012?

Calling code:

       if (!string.IsNullOrEmpty(connections.xmlconSIPPPremium))
                    {
                        try
                        {
                            DataTable dt = GetUtilityFiles(connections.xmlconSIPPPremium);
                            foreach (DataRow dr in dt.Rows)
                            {
                                File.WriteAllBytes(_rootDirectory + @"\" + dr["UtilityName"], (byte[])dr["UtilityBytes"]);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                    }
                    else if (!string.IsNullOrEmpty(connections.xmlconSSASPremium))
                    {
                        try
                        {
                            DataTable dt = GetUtilityFiles(connections.xmlconSSASPremium);
                            foreach (DataRow dr in dt.Rows)
                            {
                                File.WriteAllBytes(_rootDirectory + @"\" + dr["UtilityName"], (byte[])dr["UtilityBytes"]);
                            }
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                    }
                }

Issue:

    private DataTable GetUtilityFiles(string strCon)
    {
        DataTable dt = new DataTable();

        using (SqlConnection con = new SqlConnection(strCon))
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand("[dmm].[vertUtility_Select]", con))
                {
                    SqlDataAdapter sda = new SqlDataAdapter(cmd); 
                    sda.Fill(dt);         
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return dt;
        }
    }

Thanks!

Liam
  • 27,717
  • 28
  • 128
  • 190
Cubsoft
  • 1,147
  • 1
  • 10
  • 32
  • 1
    You're not in a loop, incidentally? Maybe you should post some code. – Knelis Jun 04 '13 at 10:08
  • No I'm not, I have posted the code. When it goes into the catch it doesn't allow me to step out of it like normal! – Cubsoft Jun 04 '13 at 10:10
  • Is this method in another try/catch block? You're throwing an exception in a `catch`, so if that's not caught, your app might stop running. – Knelis Jun 04 '13 at 10:11
  • 3
    Maybe a silly question, but why catch the exception, discard the stack trace and throw it again? Isn't it better just not to catch it at all? – Joachim Isaksson Jun 04 '13 at 10:12
  • The method from which this is being called is also in a try catch, which will then display the exception message to the user – Cubsoft Jun 04 '13 at 10:13
  • 1
    @Jacooobley In which case don't catch it in GetUtilityFiles()! – Matthew Watson Jun 04 '13 at 10:20
  • This has the same effect, when it reaches the erroring line of code I am unable to get past it. The same line of code runs over and over. – Cubsoft Jun 04 '13 at 10:21
  • Is it like the problem I had here? http://stackoverflow.com/questions/11469826/when-debugging-a-console-app-visual-studio-gets-stuck-in-an-exception-reporting – Matthew Watson Jun 04 '13 at 10:24
  • Similar yes, but I would have thought that the try catch would handle the exception, opposed to just stopping the program? – Cubsoft Jun 04 '13 at 10:28
  • 1
    @newfurniturey Just to clarify, the place that I am calling the method is wrapped within a try/catch block. Which is why I can't understand what the problem is. – Cubsoft Jun 04 '13 at 10:34
  • 1
    @Jacooobley Can you show the calling-code? – newfurniturey Jun 04 '13 at 10:36
  • @Jacooobley Can you also post the Exception? Both the message and the stack trace would be helpful. The stack trace should also point to the offending line number as well if you can let us know which line that is in the code you've posted. – newfurniturey Jun 04 '13 at 10:43
  • The exception is just that the procedure cannot be found, and once this message appears I am unable to proceed further into the code. It occurs when I am filling the data table – Cubsoft Jun 04 '13 at 10:45
  • What happens if you remove the enclosing `try{}catch{}` and leave the exception to propagate to the code that's calling the method? – Alex Filipovici Jun 04 '13 at 11:07
  • Better safe than sorry, so I'll be the one asking it: have you implemented your own `Exception` class (although it's highly improbable, as you call `GetUtilityFiles` from the same class)? – Alex Filipovici Jun 04 '13 at 11:14
  • No I haven't. I should probably look into doing this, thanks :) – Cubsoft Jun 04 '13 at 11:15

2 Answers2

1

Is it just that you want to switch off the "exception assistant"?

Tools -> options -> Debugging -> Enable the exception assistant [unchecked]

Then the exceptions won't stop execution.

Hope this helps!

mortb
  • 9,361
  • 3
  • 26
  • 44
1

When debugging there is indeed a setting in VS2012 how to you want to handle exceptions. Check under Debug -> Exceptions...

When you have the exception checked as "Thrown" the debugger will stop at that exception. Read more about "First chance exceptions": What is a "first chance exception"?

Read more about Exception handling in VS2012: http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k(vs.debug.exceptions);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0);k(DevLang-csharp)&rd=true

Also, when debugging, and you get the Exception assistand dialog, you can uncheck the option to break when that specific exception occurs.

enter image description here

Community
  • 1
  • 1
Torbjörn Hansson
  • 18,354
  • 5
  • 33
  • 42