Samantha J T Star
Samantha J T Star

Reputation: 32838

Why does my exception not get caught?

I have the following code. I enclosed it in a try block and "try" to catch exceptions:

            try
            {
                var response = query.Execute() as QueryOperationResponse;
            }
            catch (System.Net.WebException s)
            {
                var se = e;
                se = se + "aa";
            }
            catch (Exception e)
            {
                var a = e;
                a = a + "ab";
            }

When the query.Execute line executes it produces an exception and VS2010 stops on the line starting with catch (System.Net and gives the following:

System.Data.Services.Client.DataServiceQueryException was unhandled by user code
  Message=An error occurred while processing this request.
  Source=System.Data.Services.Client
  StackTrace:
       at System.Data.Services.Client.DataServiceRequest.Execute[TElement](DataServiceContext context, QueryComponents queryComponents)
       at System.Data.Services.Client.DataServiceQuery`1.Execute()

When I step through I expect it to go to the code "var se = 2;" or "var a = e". However the next line after when I step through with F11 is a jump completely out of that code block / method.

Why doesn't the exception fall into one of the catch buckets? I am totally confused.

Upvotes: 1

Views: 1669

Answers (4)

Imir Hoxha
Imir Hoxha

Reputation: 1694

If you would like to catch the exception, use DataServiceQueryException

catch (DataServiceQueryException ex)
 {
ex.Message
}

Upvotes: 0

Adam
Adam

Reputation: 16199

You can get it to step through line by line but your code actually needs to do something in the catch.

For example, just

String t = "Testing";    
t += "Another test";

That will get you in there, where you can at least look at the exception. I assume that is what you are trying to do?

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244981

A variable declaration (such as var se) is not an executable statement, so the debugger is not going to stop on that line of code.

The second problem is that you're rethrowing the exception in the second catch block. There's absolutely no reason to catch an exception and then rethrow it. If you're not going to handle it and you're just going to rethrow it, then there's no point in catching it in the first place.

Also remember (as Henk mentioned) when single-stepping code that optimizations can interfere with things. Make sure that you've built the project with all optimizations disabled before trying to understand the sequence of execution.


Other than what you're seeing single-stepping the code in the debugger, why do you think it's not going into the appropriate catch block? I've never seen exception handling code not work.

If you really want to know if you're landing in one of the catch blocks, place some type of code that has a visible effect into the block. Like a call to MessageBox.Show() for example. If you see the message box, then you've ended up inside of the catch block. If not, then you haven't.

There are times in debugging that message boxes will introduce other side effects, particularly when writing UI code, but this is not one of those cases.

Upvotes: 2

Akash Kava
Akash Kava

Reputation: 39956

DataServiceQuery.Execute() is an asynchronous method, in which you will have to either pass a delegate and call e.MarkErrorAsHandled(). Exception is shown later on and it is not in the same execution pipeline.

And e.Error will contain the thrown exception.

query.Execute( e=>{
    MessageBox.Show(e.Error.ToString());
    e.MarkErrorAsHandled();
});

or

response = query.Execute();
response.Loaded += e=>{
    MessageBox.Show(e.Error.ToString());
    e.MarkErrorAsHandled();
};

Exact names must be different but they will show up on intellisense.

Upvotes: 4

Related Questions