Hello. I am new to C# and have a question regarding catching specific exceptions. Below is my code and I would like to know if there is a better way. I would like the catch the specific OldeDbException for "invalid passsword" but how can I do this? What would I place in the Catch() statement? I used an if/then statement to catch it but is there another way in the catch block to specify an exact exception (invalid password)?
tx
try
{ dbConn.Open();}
catch (OleDbException e)
{
if (e.ErrorCode == -2147217843)
{Console.WriteLine("Invalid Password Entered"); }
else
{ Console.WriteLine(e.Message);}
}
Loading

Guest UserRank #1,422 · 1.2K reputationPosted Jul 23, 2011, 3:36 AM EDT
I would recommend adding another catch block at the end to catch and process any other exception that might occur:
try
{ dbConn.Open();}
catch (OleDbException e)
{
if (e.ErrorCode == -2147217843)
{Console.WriteLine("Invalid Password Entered"); }
else
{ Console.WriteLine(e.Message);}
}
catch (Exception exc)
{
// process all of the other exception types
}
VulpesRank #55 · 29.2K reputationPosted Jul 23, 2011, 5:02 AM EDT
There is no equivalent of the When clause in C# and so you have to catch all exceptions of a certain type and then check whether it's one you want within the catch clause itself.
Darren Murray11 reputationPosted Jul 23, 2011, 6:13 PM EDT
thanks John P...I am glad I was on the right track. I will add an additional "catch all" block...