views:

55

answers:

3

Hi All,

I have an aspx page that, on a button click, creates an instance of a serviceRefernece object. In the code behind for my page I have the call wrapped in a try/catch.

  try
        {
            var client = GetClient();
            var request = new ActiveVerificationRequestDC();
            var response = client.GetActiveVerification(request);
            DoSomethingWithTheResponse(response);      

        }
        catch (FaultException ex)
        {
            LogError(ex, MethodBase.GetCurrentMethod().Name);
            throw;

        }
        catch (Exception ex)
        {

            var args = new[] { MethodBase.GetCurrentMethod().Name, ex.Message };
            DisplayError(args);
        }

The svc file that is referenced is using pretty much the same pattern. It is calling an internal client over net.tcp. The call is wrapped in a try/catch

 try
        {
            var client = new InternalServiceClient();
            var response = client.GetActiveVerification(request);
            client.Close();
            return response;

        }
        catch (FaultException fe)
        {
            LogError(MethodBase.GetCurrentMethod().Name, fe);
            throw; 

        }

Here is my problem, how do I get errors here to bubble up to my UI catch statement? When I leave it like this, I get an unhandled exception error from visual studio. I have tried removing the throw which makes me specify a return value, and I return null. This makes the UI not work correctly. I have tried throw new Exception(fe.message) and I get the same problem with the unhandled exception. Basically my question is how can I accomplish what I need? What am I missing?

Thanks for any tips.

Cheers, ~ck in San Diego

A: 

The reason could be the throwing of another exception in your second code snippet that isn't of the type FaultException; such an exception would pass as unhandled in this situation. Maybe you should add another catch block to your second code snippet that catches exceptions of the type Exception:

    catch (FaultException fe)
    {
        LogError(MethodBase.GetCurrentMethod().Name, fe);
        throw; 
    }

    catch (Exception ex)
    {
        throw;
    }
Giu
+1  A: 

You need a separate try/catch block around your try/catch block. You have it such that you will catch a FaultException and any type of Exception, but your Exception block will not catch your throw from FaultException. So, just enclose this with another try/catch or handle it within both the FaultException catch and the Exception catch.

MCain
Or you could remove the `catch (FaultException ex)` handler on the ASPX page, depending on the logging criteria.
Rabid
A: 

Is the FaultException you are re-throwing a generic type? Are the relevant classes detailed by any FaultContract attributes accessible to all assemblies in this execution stack?

Edit: In your first code snippet ...

   catch (FaultException ex)
    {
        LogError(ex, MethodBase.GetCurrentMethod().Name);
        throw;
    }

If the unhandled error occurs on the throw statement of this handler, then @MCain is right.

Rabid