views:

379

answers:

4

Here's my problem. I have a middleware web service used by several web clients. The web service is a wrapper service that makes calls to several vendors' web services. The call to the vendor's web service is wrapped in a TryCatch but any exceptions generated aren't getting caught by my web service, they're getting caught in the client apps.

Am I missing something? Is this related to this posting?

Here's a simplified code snippet:

    Dim VendorWebservice as New VendorWebservice
    Dim VendorResponse As VendorResponse = Nothing
    Dim ClientResponse as New CustomClientResponse
    Try
        VendorResponse = VendorWebservice.VendorWebMethod
    Catch ex As Exception
        ClientResponse.ErrorMessage = ex.Message
        ClientResponse.Status = "VendorError"
        Return ClientResponse
    End Try

EDIT

To expand upon some of the details... The code runs successfully 99+% of the time. On the rare occaision that there is some issue with the vendor's web site is when this issue is occurring. I've had VS opened for both one of the web clients and the web service and can step through the code from the client to the WS and back. When I can reproduce the issue, I have stepped through the client code to where it calls our web service, then switch to the WS code and step through until it calls the vendor's code and at that point, it jumps back to the client code, without hitting the Catch block or any code after it.

Hope that helps.

EDIT

Some of the posted answers have provided avenues to research, most notibly that there are exceptions that can be created that are not derived from System.Exception. (Who knew?) But I also found out that from .NET 2.0 and later, these non-System.Exceptions are getting wrapped by .NET in a System.Exception. So in theory that should rule non-System.Exceptions out.

Additionally, from my reading, when calling a web service (like my web service is doing) theoretically only two types of exceptions should ever really be seen, System.Net.WebException and System.Web.Services.Protocols.SoapException, both of which derive from System.Exception. I don't know if it is really true if there are only 2 types of exceptions possible when calling a web service, but I'll throw it out there. :)

Still searching for an answer...

EDIT

Reproducing the error condition has proved to be elusive. Every scenario that I've thrown at the code has responded as expected, with the error being caught in the Catch block. While in theory .NET is supposed to be wrapping exceptions that do not derive from System.Exception, the only logical answer appears to agree with Joe's answer that the exception we have experienced does NOT derive from System.Exception and thus is treated as an unhandled exception.

+1  A: 

are you sure the exception happen inside the try/catch?

try moving the "as new" inside the try/catch too

Fredou
I'm positive. I've stepped through the code and make it inside the try block to the call to the vendor's web service.
Walter
A: 

It is common for web services not to throw exceptions, but instead to return some kind of error object or error message or status. If you put a breakpoint after you call VendorResponse, you can examine what was returned and see if there are any "error" or "status" properties. If so, you can test for the contents of those in your code and then throw an exception or otherwise handle the situation.

DOK
I have set a breakpoint after the call to the vendor's web site, both in the catch block and after it.
Walter
+1  A: 

Are the exceptions being thrown derived from Exception? (Does 'Exception' definitely refer to 'System.Exception')

Joe Gauterin
Good question. I'll take a look and try to post the exception.
Walter
Exceptions must be derived from System.Exception, but your 'Exception' in the catch handler might not refer to 'System.Exception'.I think it may be possible to throw exceptions that aren't derived from System.Exception using MSIL, but I'm not sure.
Joe Gauterin
If I go to the definition of the Exception in the Catch block it does display it as a System.Exception. I'll look into throwing an exception using MSIL
Walter
Could you just set your IDE to break on any exception then examine the exception to see exactly what type it is?
Joe Gauterin
Yes I can, and have. The problem is that if the exception is caught, there is no problem. The code works as expected. My issue is that we have come across situations where an exception was caught in the client not in the web service as expected. The TryCatch block was by passed somehow.
Walter
A: 

I personally find it hard to believe that an error is occurring in this code and not being caught in the try catch. Maybe put a finally and see if that executes.

First, you need a decent way to recreate your error.

The next thing I'd do is attempt to figure out exactly where in the code the error is being thrown. What I'd do would be to temporarily put in logging (Debug.Output maybe) statements after each line of code so that you can be sure which lines actually executed.

When your error occurs you'll be able to look at the output of these logs to see at least where the error is occurring. Now that you know this, it may give you a better idea of what's going on. If all of the logging statements appear, then you'll know the error isn't occurring in this block of code. My initial guess is that's what's happening.

Since nothing is falling into the catch, then you need to eliminate everything else that might be going on. If possible, put this bit of code into a tiny test harness app to see if it functions properly.

Hopefully some of that will help.

Terry Donaghe
I too was skeptical of the fact that a handled exception was not being handled. But I saw it happen in the IDE the other day when the vendor was having an issue. So I know exactly what line of code is causing the problem and all attempts so far in reproducting the issue have generated exceptions that ARE being handled. Unfortunately, I failed to get more information when it was occurring, D'OH! Do you think the vendor would mind having their issue again? :)
Walter