Have you tried this:

try
{
   db.ExecuteNonQuery(cmd);
}
catch( OracleException ex )
{
   foreach( OracleError error in ex.Errors )
   {
       Console.WriteLine( "I got Oracle error #{0}", error.Number );
   }
}

This will give you Oracle's error number.
I hope this will work. It's your second approach and you will have to switch
inside your catch block based on the error number.
Your third approach is not possible as ExecuteNonQuery only throws
OracleException exceptions. You can come up with a customized hierarchy of
exceptions, but you will need to throw them yourself after you switched on
the error number.

Cheers,
Eddie


-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Graeme Taylor
Sent: Tuesday, April 05, 2005 9:17 AM
To: [email protected]
Subject: [ADVANCED-DOTNET] Catching and Identifying Custom Oracle
Exception


Hi,

I have an Oracle stored procedure that will raise an application error
under certain conditions.  The actual errors raised may vary.

Whichever error I raise comes back as an OracleException.  I have not
found any 'number' property within the oracle exception to identify
exactly which error was thrown by my stored procedure. (The number is in
the message but parsing it seems messy).

In the code below, I have included 3 examples - the first one I don't want
to have to do, the second one seems more achievable and the third would be
the ideal but I've no idea how to do the second or third examples.


CODE
// Example 1 - last resort

try{
    db.ExecuteNonQuery(cmd);
} catch ( OracleException ex){

    if (ex.Message == "MyCustomError1"){
        // Do some stuff
    } elseif (ex.Message == "MyCustomError2"){
        // Do something else
    }

    //     and so on ...
}

// Example 2 - possible solution
// Not sure how to retrieve error number though

try{
    db.ExecuteNonQuery(cmd);
} catch ( OracleException ex){

    if (ex.Number == 1){
        // Do some stuff
    } elseif (ex.Number == 2){
        // Do something else
    }

    //     and so on ...
}

// Example 3 - would really like to have this, but
//  not sure how to map the custom oracle exception to
//  my exception class
public class CustomException1 : Exception{
    // Implementation ommitted
}

// Usage
try{
    db.ExecuteNonQuery(cmd);
} catch ( CustomException1 ex){
    // do something
} catch (CustomException2 ex){
    // do something else
}

Would be very gratefuul for any hints or tips.

Thanks,

Graeme

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to