C# error “Not all code paths return a value”

后端 未结 8 1861
长发绾君心
长发绾君心 2020-12-12 06:05

I translated this part of the code from vb to c# and giving me this error message. \"Not all code paths return a value\". What is the problem? Thanks in advance.

<         


        
相关标签:
8条回答
  • 2020-12-12 06:34

    You can resolve this issue by :

    1. change the function return to VOID (if you does not return something)
    2. give return keyword with variable name before end function
    0 讨论(0)
  • 2020-12-12 06:38

    If an exception occurs in your try block before the return statement, the catch is executed and that does not return anything, because you did not tell it to.

    You can do one of these:

    • Return a value from the catch block. Do this only if it makes sense and you have a sensible value you can return. Be aware that returning null is a usual source of bugs and there are patterns out there to avoid just that.
    • Re-throw the exception that occurred, if you cannot do anything at this point about it (and return an object that makes sense). You can do this by adding a line that says: throw;
    • Throw a different error - You can package the original exception in a new one, providing extra details about the context, if necessary.
    0 讨论(0)
提交回复
热议问题