I’m trying to write a single if-else block within a catch method, but when I get the error that ‘not all code paths return type int.’
Essentially, I’m writing a method that saves a UserName and Password to a Sql CE database, and I want to test for one specific error. (UserName already exists.) The UserName column is unique, so it will reliably throw a SqlCeException with NativeError = 25016. I want to display that with an error message, otherwise handle other exceptions in a more generic way.
My code is:
try
{
//insert command (This is where the duplicate column error is thrown)
//Select @@Identity & Return it
}
catch (SqlCeException ex)
{
if (ex.NativeError == 25016)
MessageBox.Show("Username already in use.");
else
//whatever
}
I’ve tried showing a different, more generic message and get the no return error.
I’ve tried throwing a new exception and catching it in another block. Same error.
I’ve tried returning -1 (as a sort of flag value). Same error.
There doesn’t appear to be a more specific error than SqlCeException for this specific case (Though I ma have just failed me debug-fu).
Any creative work-arounds?
Worst case, I’ll write a method that checks the DB for duplicate usernames before calling this method.
It looks like your method returns an
int. You need to return a value of typeintfrom your catch block.