By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

The out parameter 'errMsg' must be assigned to before control leavesthe current method

P: n/a
I get this error:

The out parameter 'errMsg' must be assigned to before control leaves the
current method

when compiling the following code:
public void ExecuteCommands(DDLCommands commands, out string
errMsg)
{
try
{
foreach (Command cmd in commands)
{
errMsg = "";
m_cmd.CommandText = cmd.Expression();
m_cmd.ExecuteNonQuery();
}
}
catch (SqlException e)
{
errMsg = e.Message;
Console.WriteLine(e.Message);
}
}
I am initing the errMsg variable in the method body - so I dont
understand the error msg - what am I missing ?
Jul 8 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
It doesn't trust you that there is always a command... what if commands is
an empty list?

Just set errMsg to blank at the top somewhere. Note also that your code will
only keep an error if it happens on the last command. Also, I'd prrobably
recommend throwing an exception rather than using an "out"...

The simplest way would be to remove the handling entirely and let the first
exception bubble to the caller; if (for whatever reason - it sounds very
dangerous to me...) you need to run all the commands and present one
combined error (if any), then perhaps:

// not recommended
StringBuilder sb = null;
foreach(Command cmd in commands)
{
try {
// blah
} catch (SqlException ex) {
if(sb == null) sb = new StringBuilder();
sb.AppendLine(ex.Message);
}
}
if(sb != null) throw new InvalidOperationException(sb.ToString());

Marc
Jul 8 '08 #2

P: n/a
(2b|!2b)==? wrote:
I get this error:

The out parameter 'errMsg' must be assigned to before control leaves the
current method

when compiling the following code:
public void ExecuteCommands(DDLCommands commands, out string
errMsg)
{
try
{
foreach (Command cmd in commands)
{
errMsg = "";
m_cmd.CommandText = cmd.Expression();
m_cmd.ExecuteNonQuery();
}
}
catch (SqlException e)
{
errMsg = e.Message;
Console.WriteLine(e.Message);
}
}
I am initing the errMsg variable in the method body - so I dont
understand the error msg - what am I missing ?
You are initialising the string in code that is not guaranteed to run.

The collection could be empty, or the commands.GetEnumerator call could
possibly throw an exception, which would mean that the code in the loop
would not run at all. You have to initialise the variable before the loop.

The compiler might not be able to determine that it's safe to initialise
the variable as long as you do it inside the try block. You should just
initialise it the first thing that you do in the method.

--
Göran Andersson
_____
http://www.guffa.com
Jul 8 '08 #3

P: n/a
Göran Andersson wrote:
(2b|!2b)==? wrote:
>I get this error:

The out parameter 'errMsg' must be assigned to before control leaves
the current method

when compiling the following code:
public void ExecuteCommands(DDLCommands commands, out string
errMsg)
{
try
{
foreach (Command cmd in commands)
{
errMsg = "";
m_cmd.CommandText = cmd.Expression();
m_cmd.ExecuteNonQuery();
}
}
catch (SqlException e)
{
errMsg = e.Message;
Console.WriteLine(e.Message);
}
}
I am initing the errMsg variable in the method body - so I dont
understand the error msg - what am I missing ?

You are initialising the string in code that is not guaranteed to run.

The collection could be empty, or the commands.GetEnumerator call
could possibly throw an exception, which would mean that the code in
the loop would not run at all. You have to initialise the variable
before the loop.
If GetEnumerator throws an exception which causes execution to leave the
function, there's no requirement to assign output variables.
(And the case of catching and discarding the exception is properly handled.)

It's the possibility of an empty collection that's causing the problem.
>
The compiler might not be able to determine that it's safe to
initialise the variable as long as you do it inside the try block.
You should just initialise it the first thing that you do in the
method.

Jul 8 '08 #4

P: n/a
Marc Gravell wrote:
It doesn't trust you that there is always a command... what if
commands is an empty list?

Just set errMsg to blank at the top somewhere. Note also that your
code will only keep an error if it happens on the last command. Also,
No, it'll stop the loop on the first error.
I'd prrobably recommend throwing an exception rather than using an
"out"...
The simplest way would be to remove the handling entirely and let the
first exception bubble to the caller; if (for whatever reason - it
sounds very dangerous to me...) you need to run all the commands and
present one combined error (if any), then perhaps:

// not recommended
StringBuilder sb = null;
foreach(Command cmd in commands)
{
try {
// blah
} catch (SqlException ex) {
if(sb == null) sb = new StringBuilder();
sb.AppendLine(ex.Message);
}
}
if(sb != null) throw new InvalidOperationException(sb.ToString());

Marc

Jul 8 '08 #5

P: n/a
You are right; I read it as being foreach { try {...} catch {...} }, but it
is actually try { foreach {...} } catch {...}

My bad...

Marc
Jul 8 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.