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 UserPosted Jul 23, 2011, 7:36 AM
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
}
Darren MurrayPosted Jul 23, 2011, 10:13 PM
thanks John P...I am glad I was on the right track. I will add an additional "catch all" block...
VulpesPosted Jul 23, 2011, 9:02 AM
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.