2

I am new to C# and I'm struggling with very basic stuff... for instance I am now trying to compare an exception that I KNOW will print exactly the phrase "ORA-28007: the password cannot be reused" if I use Response.Write(ex.Message). However, in the block below, the comparison between ex.Message and the string just provided fails and it returns that Unhandled Exception I've put in the else clause... How should I be comparing the exception with the string?

catch (Exception ex)
{
   if (ex.Message == "ORA-28007: the password cannot be reused")
   {
     Response.Write(ex.Message);
     // TODO : Correct the exception to be presented in the popup instead of the same page.
     // display = "The password cannot be reused! Pick a new one.";
     // ClientScript.RegisterStartupScript(this.GetType(), 
     //     "Error.", "alert('" + display + "');", true);
   }
   else
   {
     Response.Write("Unhandled exception: " + ex.Message);
   }
}
3
  • 1
    your error message may be slightly different. try ex.Message.Contains("ORA-28007") Commented Dec 23, 2016 at 19:06
  • 1
    normally Exceptions has an HResult property with a numeric value that you can check. Commented Dec 23, 2016 at 19:25
  • 5
    Why dont you put a break point on the If() statement and check the result of ex.Message in the debugger ? Commented Dec 23, 2016 at 19:27

1 Answer 1

6

If you're using the Oracle Data Provider for .NET you can catch OracleExceptions instead of Exceptions and get some more details by looking at the Errors property, which gives a list of OracleError objects:

catch(OracleException oex)
{
    foreach (OracleError error in oex.Errors) 
    {
        Console.WriteLine("Error Message: " + error.Message);
        Console.WriteLine("Error Source: " + error.Source);

        if(error.Number == 28007) 
        {
            // do specific stuff
        }       
     }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you all for the answers and help! I used the tip from L.B (ex.Message.Contains("ORA-28007"))...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.