简体   繁体   中英

stackoverflow exception

public static int ExecuteNonQuery(String procedure, params SqlParameter[] args)
{
    if (args == null) throw new ArgumentNullException("args");
    else
    return ExecuteNonQuery(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.

该函数将无限递归,因为没有终止条件:当使用非null args参数调用时,它只使用indentical参数调用自身,并且除了耗尽堆栈空间之外没有任何东西可以阻止此无限递归。

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);
 }

因为你在return语句中称呼自己。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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