1

I have the following method called asynchronously. I want to know how can I convert the System Exception (that I might encounter during SQL operations) into a FaultException.

Here is the method:

public List<Product> GetProductDetails(int productKey)
{
    try
    {
        using (SqlConnection con = new SqlConnection(_connectionString))
        {
            SqlCommand cmd = new SqlCommand("usp_Get_ProductDetails", con);
            ........
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    ......
                }
            }
        }
    }
    catch(Exception ex) 
    {
        //How can I convert this Exception ex into FaultException and then throw to client?
        //throw new FaultException(new FaultReason(new FaultReasonText()), new FaultCode());

    }
}
Huma Ali
  • 1,759
  • 7
  • 40
  • 66
  • 2
    Can you not just catch a `FaultException`? – Steve Feb 09 '16 at 11:54
  • Probably this link may be helpful. https://msdn.microsoft.com/en-us/library/ms732013(v=vs.110).aspx – Pankaj Kapare Feb 09 '16 at 11:56
  • http://stackoverflow.com/questions/17097839/how-to-handle-wcf-fault-exception why not use the anwser from here? Passing the type of the exception to the fault exception – misha130 Feb 09 '16 at 11:57
  • @Steve what would be the format of catching and then throwing `FaultException`? I want to show the `FaultException.Reason` on a MessageBox on the client side code. Can you post answer? – Huma Ali Feb 09 '16 at 12:04
  • @HumaAli see my answer – Steve Feb 09 '16 at 12:06

2 Answers2

0

You can catch multiple exceptions (and in fact is good practice to do so if a given exception is reasonably likely to occur), then do something with each.

For example:

try
{
    using (SqlConnection con = new SqlConnection(_connectionString))
    {
        SqlCommand cmd = new SqlCommand("usp_Get_ProductDetails", con);
        .....
    }
}
catch(FaultException faultEx)
{
    // code to send to client
}
catch(Exception ex) 
{
    // code to do something else
}
Steve
  • 9,335
  • 10
  • 49
  • 81
  • the code inside `try` shouldn't thow `FaultException`. – Amit Kumar Ghosh Feb 09 '16 at 12:10
  • 1
    @HumaAli that depends entirely on where and how you're using the method, is it in a controller, model, somewhere else? I'm just answering the question given. – Steve Feb 09 '16 at 12:10
  • @Steve I just want to know how to send FaultException message after catching it – Huma Ali Feb 09 '16 at 12:11
  • 1
    @AmitKumarGhosh I removed code from the original example for brevity. His code could well throw that exception. – Steve Feb 09 '16 at 12:12
  • @HumaAli you should probably edit your question to say that, currently you're just asking how to get the `FaultException`. – Steve Feb 09 '16 at 12:12
  • 1
    If you want the `FaultException` to be thrown but other exceptions to be caught then `throw;` in the catch-block for `FaultException`. If you aren't reasonably expecting (or able to usefully cope with) any other exception then just don't have the `try` at all. – Jon Hanna Feb 09 '16 at 12:29
  • This appears to be missing the point of the question: how to make a FaultException out of an Exception? The question is about converting or wrapping a .NET native exception as SQL throws to a WCF / SOAP exception instance FaultException. – David Burg Jul 30 '19 at 22:23
0
catch(Exception ex) 
{
    throw new FaultException(ex.Message);
}
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24
  • 4
    Throwing a new exception instance is bad practice because you will lose the entire stack track up to that point: http://stackoverflow.com/questions/2999298/difference-between-throw-and-throw-new-exception – Steve Feb 09 '16 at 12:13
  • 1
    @Markus this didn't wrap the exception, just the message, so it is indeed lost. Of course, sometimes one wants that *precisely* to lose the stack trace. In this case though it's a different `FaultException` to the one originally thrown, so there's not much point. – Jon Hanna Feb 09 '16 at 12:27
  • well I thought it's the error message OP is concerned about. I know about stack traces being lost and etc. – Amit Kumar Ghosh Feb 09 '16 at 13:41