none
Catching the Oracle constraint exception in C#.NET RRS feed

  • Question

  •  

    I am using 2 catch block to catch  constraint exception  and normal database errors with Oracle database  when a new row is added or changed, like this:

     

     

    try

    {

    //Code

    }

    catch (DbException dbExp)

    {

    //Log the error

    }

    catch (ConstraintException cnExp)

    {

    //Log the error

    }

     

    But when it violates the constraints also it catches in the DbException block.Anybody faced the same iisue?Please advice me on this  .

    Monday, September 24, 2007 2:10 AM

Answers

All replies

  • Is the problem with the SQL or the fact that you catch the error?
    Monday, September 24, 2007 2:31 AM
  • I want to catch the constrain error separately ,but i am not able to do that

    Monday, September 24, 2007 2:35 AM
  • Reverse the order of the catches, because I surmise, the constraint error is based on the other error. The catch mechanisim drops into the base because it is valid.  (But not what you want)

    By having the derived exception first followed by the base exception, that will achieve the affect you are looking for.
    Monday, September 24, 2007 2:37 AM
  • Thanks for your immediate response. Let me try this and I will update you.

    Monday, September 24, 2007 2:56 AM
  • I tried as you suggested, but still it is going to the DbException catch block. I am confused why it behaves like this. You have any idea? The latest code skeleton as follows

     

    try

    {

    //Code

    }

    catch (ConstraintException cnExp)

    {

    //Log the error

    }

    catch (DbException dbExp)

    {

    //Log the error

    }

     

    This is what you suggested..right?

     

    Monday, September 24, 2007 6:33 AM
  • Is the ConstraintException the one that is actuall thrown? If it is and its being caught by the dbException, then in the DbException code block you could do this

    ConstraintException cnExp = dbExp as ConstraintException;

    if (cnExp != null)
    {
        do work within.
    }

    But I am guessing that ConstraintException may be different from the actual constraint being caught...which happens to be another flavor of a constraint exception, but not thrown that way.
    Monday, September 24, 2007 10:39 AM
  • Hi Sarmak,

     

    I checked MSDN document about DbException Class - http://msdn2.microsoft.com/en-us/library/system.data.common.dbexception(vs.80).aspx and found it was an abstract class used as the base class for provider-specific exception class implementations. You can't get its instance.

    ConstraintException Class represents the exception that is thrown when attempting an action that violates a constraint.

     

    Based on my understanding for your questions, if you would like to catch Oracle database exception, I suggest you to change your exception handling sample codes as follows:

    Code Block

    try

    {

    //Code

    }

    catch (System.Data.OracleClient.OracleException dbExp)

    {

    //Log the error

    }

    catch (ConstraintException cnExp)

    {

    //Log the error

    }

     

     

    Hope this helps,

     

    Regards,

    Citizens on the earth

    Thursday, September 27, 2007 3:13 AM