开发者

stackoverflow exception

开发者 https://www.devze.com 2022-12-15 10:32 出处:网络
public static int ExecuteNonQuery(String procedure, params SqlParameter[] args) { if (args == null) throw new ArgumentNullException(\"args\");
public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
{
    if (args == null) throw new ArgumentNullException("args");
    else
    return Exec开发者_运维问答uteNonQuery(procedure, new SqlParameter[] { });
}

Why getting recursive function and throwing StackOverFlow Exception when calling this above method.(while the argument contains 5 values)


Don't confuse an empty array with a null array. You're calling the same method again with an empty array, but the only check you have to cease this function is to check for a null array, and throw an exception. You need something like:

if (args.length == 0) {
   // bail out somehow
}

(after your null check, to prevent NPEs)


That is because overload resolution picks the same ExecuteNonQuery method, so you are essentially calling the same method over and over again.

Your method takes a SqlParameter[] (the params part is just syntactic sugar), and you are calling the same method again, with a SqlParameter[] as the second argument.


The function will recurse infinitely because there is no termination condition: when called with a non-null args parameter it just calls itself with indentical parameters, and there is nothing that stops this infinite recursion other than running out of stack space.


Perhaps you wanted something like

public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
{
  if (args == null) 
      return ExecuteNonQuery(procedure, new SqlParameter[] { });

  // Do some stuff here where args can be assumed != null
}


Your args variable is never null; You should to code:

return ExecuteNonQuery(procedure, null);

or to create an overload which doesn't have an args argument.


you need to check if the list has a length of 0, not if it is null. Or you could pass it null, like so:

 public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
 {
            if (args == null) throw new ArgumentNullException("args");
            else
            return ExecuteNonQuery(procedure, null);
 }


Because you call yourself in the return statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消