简体   繁体   English

C#if-else在catch语句中

[英]c# if-else within catch statement

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.' 我正在尝试在catch方法中编写一个if-else块,但是当我看到“并非所有代码路径都返回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和Password保存到Sql CE数据库的方法,并且我想测试一个特定的错误。 (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. (UserName已经存在。)UserName列是唯一的,因此它将可靠地引发NativeError = 25016的SqlCeException。我想显示一条错误消息,否则将以更通用的方式处理其他异常。

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). 我尝试返回-1(作为一种标记值)。 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). 在这种特定情况下,似乎没有比SqlCeException更具体的错误(尽管我刚刚使我的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 . 看来您的方法返回一个int You need to return a value of type int from your catch block. 您需要从catch块返回int类型的值。

  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  
   }
   return -1;
  }

Sounds like your function returns an int value. 听起来您的函数返回一个int值。

In your try block you get and return an integer value. try块中,您获取并返回一个整数值。 That's fine. 没关系。

In the catch block you do not mention returning an integer. catch块中,您不会提及返回整数。 This is the problem. 这就是问题。

So when the function takes the exception path, it doesn't have an integer to pass back causing the error. 因此,当函数采用异常路径时,它没有返回值的整数会导致错误。

Your whole function need to return value. 您的整个函数需要返回值。 Since you are eating exception in try-cathc you need to have return of some sort at the end of the function. 由于您在try-cathc中正在吃异常,因此您需要在函数末尾返回某种返回值。

Use finally block to return int: 使用finally块返回int:

 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
  }
  finally
  {
    return -1;
  }

If try block return Identity value then finally return -1 will not effect. 如果try块返回了Identity值,那么最终return -1将无效。 Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM