none
Can I rely on the HRESULT code to judge actual exception type? RRS feed

  • Question

  • Hi,

    Currently we want to catch the unique constraint violation exception during updating and wrap it up.

    However, Entity Framework already wrap this exception with its DbUpdateException. So my question is, how can we make sure the DbUpdateException is due to unique constraint violation, without parsing the inner exception? Can we trust the HRESULT code in the DbUpdateException?

    Friday, January 18, 2013 4:52 AM

Answers

  • Hi Michael,

    No you cannot use the HRESULT code as it is specific to the DbUpdateException and not to the inner exception which we are trying to catch. There is no way we can identify the unique constraint violation exception without parsing the inner exception.

    To confirm this you can create a sample application which first violates Primary key constraint and then the Unique key constraint and you will notice that the HRESULT is same in both cases. The reason for this being that the HRESULT is specific to the DbUpdateException and not the inner exception. (I tried this at my end)

    Can you let me know the reason why you do not want o parse the inner exception? 

    Thanks,

    Kunal (MSFT)

    Wednesday, January 30, 2013 3:11 PM

All replies

  • Hi Michael,

    Welcome to the MSDN forum.

    I am trying to involve a senior expert into your thread. Please wait for the response. Sorry for any inconvenience.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 22, 2013 7:42 AM
  • Hi Michael,

    No you cannot use the HRESULT code as it is specific to the DbUpdateException and not to the inner exception which we are trying to catch. There is no way we can identify the unique constraint violation exception without parsing the inner exception.

    To confirm this you can create a sample application which first violates Primary key constraint and then the Unique key constraint and you will notice that the HRESULT is same in both cases. The reason for this being that the HRESULT is specific to the DbUpdateException and not the inner exception. (I tried this at my end)

    Can you let me know the reason why you do not want o parse the inner exception? 

    Thanks,

    Kunal (MSFT)

    Wednesday, January 30, 2013 3:11 PM
  • Hi Kunal, 

    Thanks for your confirmation. We tried to avoid parsing inner exceptions simply because we don't want to make the error handling logic too complicated. I was looking into the implementation of EF update logic, and found the underlying code throws SQLException, which can be caused by various reasons: execution timeout, lost connection, constraint violation etc. Currently we just take care of the first level exception... However, maybe we should recursively log the whole exception hierarchy for better debugging purpose.

    Wednesday, February 27, 2013 11:44 AM