Hi I’m implementing a local database inside of my WP7 app and was wondering how to handle different types of SqlCeExceptions that could arise when an operation is run on the database. For example I would want to show a certain error message if an attempt to delete an item bound by a foreign key was executed and a different error for lets say a random unhandled exception.
To me it would seem that maybe the Exception could be passed in a switch statment inside the catch block like so:
switch(myException)
{
case ForeignKeyException:
//Handle it this way...
break;
case UnhandledException:
//Handle it another way...
break;
}
Does anyone know how to implement this or maybe point me somewhere where I can learn how to handle these exceptions seperately?
While I do not have the answer, I have always solved this by never letting exceptions happen. I always check before inserting that the data is valid. I make sure that all unique indexes are respected (user name, …) and that all foreign key problems have been dealt with. This is, IMHO, the only sane way to do this. I would not like producing exceptions and handling them because I could never be really sure that I do not catch too many exceptions and thereby hiding real bugs.